运行Apache / WSGI时,Django URL解析不起作用

时间:2011-03-10 11:23:01

标签: python django apache wsgi

当我使用Djangos内置服务器运行我的应用程序时,一切正常。但是当我尝试通过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')),
                       )

立即行动。

4 个答案:

答案 0 :(得分:2)

删除 WSGIScriptAlias /django上的尾随斜杠为我工作

答案 1 :(得分:1)

在Apache配置中将尾部斜杠添加到URL路径前缀:

WSGIScriptAlias /project/ /home/user/project/apache/project.wsgi

答案 2 :(得分:1)

解决方案1,编辑你的apache配置:

WSGIScriptAlias / /home/user/project/apache/project.wsgi

解决方案2,编辑你的urls.py:

from django.conf.urls.defaults import *
from django.conf import settings

urlpatterns = patterns('',
                   (r'^/project/media/(?P<path>.*)$', 'django.views.static.serve', 
                    { 'document_root': settings.MEDIA_ROOT }),
                   (r'^/project/app/live/', include('project.app.urls', app_name='live')),
                   )

另外,如果你使用的是apache,你可能不希望通过django提供静态内容:

Alias /media /path/to/media/root
<Location /media>
SetHandle None
</Location>

答案 3 :(得分:1)

讨厌带来旧线程,但是,我通过确保我的apache2配置将python路径包含在引号中来修复此问题,如:

WSGIScriptAlias / /home/user/django_project/wsgi.py
WSGIPythonPath "/home/user:/home/user/.virtualenvs/djangodev/lib/python3.4/site-packages"

我在关注this tutorial时使用了Django 1.9,python3.4和virtualenv。

更新:抓一点,我是maroon并且读得不够好看我需要在守护进程模式下使用mod_wsgi以避免每次我都需要重启apache做出改变。

WSGIDaemonProcess example.com python-path="/home/user:/home/user/.virtualenvs/djangodev/lib/python3.4/site-packages"
WSGIProcessGroup example.com
WSGIScriptAlias / /home/user/django_project/wsgi.py process-group=example.com