Django中没有名为views的模块错误。是由于Apache,wsgi.py还是其他?

时间:2014-02-18 19:07:18

标签: django apache django-wsgi

我有一个Django应用程序,可以在localhost上正常运行。但是当我在服务器上尝试它时,它会给我以下错误(如DEBUG输出中所示):

No module named views

Request Method:     GET
Request URL:    url_to_site/accounts/login
Django Version:     1.6.1
Exception Type:     ImportError
Exception Value:    

No module named views

Exception Location:     /opt/dev/site/smsmessages/views.py in <module>, line 1
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/opt/dev/myfolder/project/app',
 '/opt/dev/myfolder/project',
 '/usr/lib64/python26.zip',
 '/usr/lib64/python2.6',
 '/usr/lib64/python2.6/plat-linux2',
 '/usr/lib64/python2.6/lib-tk',
 '/usr/lib64/python2.6/lib-old',
 '/usr/lib64/python2.6/lib-dynload',
 '/usr/lib64/python2.6/site-packages',
 '/usr/lib64/python2.6/site-packages/gtk-2.0',
 '/usr/lib/python2.6/site-packages']

堆栈跟踪的一部分如下:

/opt/dev/myfolder/project/app/urls.py in <module>
    url(r'^messages/', include('smsmessages.urls', namespace='smsmessages', app_name='smsmessages')),

[some more stack trace output here...]

/opt/dev/myfolder/project/smsmessages/urls.py in <module>
    from smsmessages.views import MessageCreateView, MessageListView, MessageUpdateView, MessageDeleteView

[...]

/opt/dev/myfolder/project/smsmessages/views.py in <module>
    from braces.views import LoginRequiredMixin 

我在此应用中使用Django-braces。但令我感到困惑的是,当我跑步时应用程序没有任何故障:

$ python manage.py runserver

在同一台服务器上:

$ curl mysite
<site's content is returned here without a glitch>

当我这样做时,导入braces.views似乎也有效:

$ python manage.py shell
    (InteractiveConsole)
    >>> from braces.views import *
    >>> print "no problem here"

只有当我作为服务器返回Apache时,我才会遇到上述错误(即使使用curl)。

这让我想知道它是wsgi.py还是/和Apache('2.2.15(Red Hat)')配置导致问题。关于wsgi和Apache的DEBUG消息返回的部分元信息如下。

mod_wsgi.listener_port  '80'
mod_wsgi.listener_host  ''
SERVER_SOFTWARE     'Apache/2.2.15 (Red Hat)'
SCRIPT_NAME       u''
mod_ssl.var_lookup  ''
mod_wsgi.handler_script     ''
SERVER_SIGNATURE    '<address>Apache/2.2.15 (Red Hat) Server at site.com Port 80</address>\n'
REQUEST_METHOD  'GET'
PATH_INFO   u'/accounts/login'
SERVER_PROTOCOL     'HTTP/1.1'
mod_wsgi.request_handler    'wsgi-script'
wsgi.url_scheme     'http'
PATH_TRANSLATED     '/opt/dev/myfolder/project/app/wsgi.py/accounts/login'
wsgi.multiprocess   True
mod_wsgi.input_chunked  '0'
DOCUMENT_ROOT   '/var/www/html'
mod_wsgi.process_group  ''
SCRIPT_FILENAME     '/opt/dev/myfolder/project/app/wsgi.py'

wsgi.py的内容是:

import os, sys
sys.path.append('/usr/lib64/python2.6/site-packages/')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Apache配置如下:

WSGIPythonPath /opt/dev/myfolder/project/

<Directory /opt/dev/myfolder/project/app>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

<VirtualHost *:80>
Alias /static/ /opt/dev/myfolder/app/static/
ServerAdmin email@email.com
ServerName site.com
ErrorLog logs/site.com-error_log
CustomLog logs/site.com-access_log common
WSGIScriptAlias / /opt/dev/myfolder/project/app/wsgi.py

我有什么错误或遗失的吗?有没有人见过这个错误?我已经探索了所有可能的东西(包括关于No module named views的StackOverflow上的那些,但我真的很感激,如果有人在Django,Apache,wsgi和Django-braces方面有经验可以告诉我这里可能有什么问题。谢谢你非常喜欢。

1 个答案:

答案 0 :(得分:1)

我对django-braces没有任何经验,但我认为这是循环依赖的问题。我发现循环依赖的问题与mod_wsgi / Apache而不是Django dev服务器有关,因为Django可以不同的方式导入模块(按不同的顺序),具体取决于您的站点的配置或部署方式。

这听起来与Baffled: Django "could not import app.views" but can import app, in WSGI?非常相似,例如。