我有一个在本地主机上运行良好的 Django 应用程序。但是当我在服务器上尝试它时,它给了我以下错误(如 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)') 配置导致了问题。DEBUG 消息返回的有关 wsgi 和 Apache 的部分元信息如下。
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
有什么我做错或遗漏的事情吗?有没有人看到这个错误?我已经探索了所有我能做的事情(包括 StackOverflow 上关于 的那些No module named views
,但如果有人在 Django、Apache、wsgi 和可能的 Django-braces 方面有经验的人能告诉我这里可能出了什么问题,我将不胜感激。非常感谢。