【问题标题】:Django + Apache wsgi = paths problemDjango + Apache wsgi = 路径问题
【发布时间】:2010-05-13 09:30:54
【问题描述】:

我有这个生成界面语言选项菜单的视图

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})

当我使用开发服务器 (python manage.py runserver ...) 时,它可以正常工作。但是当我将我的应用程序移植到 apache wsgi 服务器时出现错误“No languages configured or incorrect language files!

这是我的 Apache 配置

<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()

这看起来像是路径配置的问题,但我被困在这里......

【问题讨论】:

  • 如果'SetHandler None' 指令和相应的周围'Location' 为空,则不需要它。只有在将 mod_python 用于某些配置时才需要该指令,而 mod_wsgi 不需要该指令。

标签: python django path wsgi


【解决方案1】:

如果您在lang_menu 中调用它,这是否为您提供了正确的路径?

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

如果这确实指向您的视图模块所在的目录,您可以从那里构建以创建绝对路径,例如:

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

【讨论】:

    【解决方案2】:

    问题可能出在os.path.isfile("gui/%s.py" % lang) 行。您在这里使用的是相对路径。改用绝对路径应该没问题。

    其他一些建议:

    1. 不要使用exec 导入文件。请改用__import__
    2. 不要寻找文件来决定配置!它缓慢且不可靠。例如,将数据存储在数据库中。

    【讨论】:

      【解决方案3】:

      很难看出发生了什么,因为尽管您在循环中存储了有用的错误,但最后却用一般错误覆盖了它们。实际列出遇到的错误会更有帮助。

      我还会质疑为什么您要手动管理语言文件,而不是使用内置的国际化/本地化处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        • 2011-03-04
        • 2011-11-24
        • 2012-05-31
        • 1970-01-01
        • 2016-02-16
        • 2020-05-29
        相关资源
        最近更新 更多