【发布时间】:2011-07-12 15:40:47
【问题描述】:
当我使用 Django 内置服务器运行我的应用程序时,一切正常。但是当我尝试通过 Apache 和 WSGI 运行时,URL 不再被识别,但它在 urls.py 文件中。
我得到的错误页面是这样的:
Page not found (404)
Request Method: GET
Request URL: http://localhost/project/app/live/
Using the URLconf defined in project.urls, Django tried these URL patterns, in this order:
^media/(?P<path>.*)$
^app/live/
The current URL, app/live/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
如您所见,URL (app/live/) 就在顶级 urls.py 文件的 URL 模式中。 Apache errors.log 文件中也没有错误。
我的 urls.py:
from django.conf.urls.defaults import *
from django.conf import settings
urlpatterns = patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root': settings.MEDIA_ROOT }),
(r'^app/live/', include('project.app.urls', app_name='live')),
)
我的 WSGI 文件:
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../..')
os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
最后是我的 Apache 配置:
WSGIDaemonProcess project processes=2 maximum-requests=500 threads=1
WSGIProcessGroup project
WSGIScriptReloading On
WSGIScriptAlias /project /home/user/project/apache/project.wsgi
编辑:
在 RegexURLResolver 中放入一些调试输出后,我看到它尝试解析 app/live/ 和 project/app/live/。
所以我将 urls.py 文件更改为:
from django.conf.urls.defaults import *
from django.conf import settings
urlpatterns = patterns('',
(r'^app/live/', include('project.app.urls', app_name='live')),
(r'^project/app/live/', include('project.app.urls', app_name='live')),
)
现在工作。
【问题讨论】:
-
2) 因为否则它找不到设置模块。
ImportError: Could not import settings 'project.settings' (Is it on sys.path? Does it have syntax errors?): No module named project.settings。也许我的整个项目/应用设置配置错误? -
哦,我明白了,你有一个额外的 WSGI 文件的“apache”子目录。非虚拟机
-
我看不出你的配置有什么问题。你有最新的 mod_wsgi/Django 版本吗?您也可以尝试打印调试
RegexURLResolver.resolve。