views:

231

answers:

3

Hello. I have this view which generates interface language options menu

def lang_menu(request,language):

    lang_choices = []
    import os.path

    for lang in settings.LANGUAGES:
        if os.path.isfile("gui/%s.py" % lang) or os.path.isfile("gui/%s.pyc" % lang):
            langimport = "from gui.%s import menu" % lang
            try:
                exec(langimport)
            except ImportError:
                lang_choices.append({'error':'invalid language file'})
            else:
                lang_choices.append(menu)
        else:
            lang_choices.append({'error':'lang file not found'})

    t = loader.get_template('gui/blocks/lang_menu_options.html')

    data = ''

    for lang in lang_choices:
        if not 'error' in lang:
            data = "%s\n%s" % (data,t.render(Context(lang)))

    if not data:
        data = "Error! No languages configured or incorrect language files!"

    return Context({'content':data})

When I'am using development server (python manage.py runserver ...) it works fine. But when I ported my app to apache wsgi server I've got error "No languages configured or incorrect language files!"

Here is my Apache config

<VirtualHost *:9999>

WSGIScriptAlias / "/usr/local/etc/django/terminal/django.wsgi"

<Directory "/usr/local/etc/django/terminal">
    Options +ExecCGI
    Allow From All
</Directory>

Alias /media/ "/usr/local/lib/python2.5/site-packages/django/contrib/admin/media/"
<Location /media/>
    SetHandler None
</Location>

<Directory "/usr/local/lib/python2.5/site-packages/django/contrib/admin/media/>
    Allow from all
</Directory>

Alias /static/ "/usr/local/etc/django/terminal/media/"
<Location /static/>
    SetHandler None
</Location>

ServerName *******
ServerAlias *******
ErrorLog /var/log/django.error.log
TransferLog /var/log/django.access.log

</VirtualHost>

django.wsgi:

import os, sys
sys.path.append('/usr/local/etc/django')
sys.path.append('/usr/local/etc/django/terminal')
os.environ['DJANGO_SETTINGS_MODULE'] = 'terminal.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

It's look like as problem with path configuration but I'm stuck here ...

+1  A: 

It's hard to see what's going on, because although you are storing useful errors in your loop you are then overwriting them all with a generic error at the end. It would be more helpful to actually list the errors encountered.

I would also question why you're managing language files manually, instead of using the built-in internationalization/localization handling.

Daniel Roseman
+1  A: 

The problem may be in the os.path.isfile("gui/%s.py" % lang) line. You are using a relative path here. Use an absolute path instead and you should be fine.

Some other pieces of advice:

  1. Don't use exec to import files. Use __import__ instead
  2. Don't look for files to decide over a configuration! it's slow and unreliable. Store the data in a database, for example.
Olivier
+1  A: 

Does this give you the right path if you call it in lang_menu?

os.path.abspath(os.path.dirname(__file__))

If this indeed points to the directory your view module lives in, you can build from there to create an absolute path, e.g.:

here = lambda *x: os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
if os.path.isfile(here('gui', '%s.py' % lang)):
    ...
Mark van Lent