简体   繁体   English

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

[英]Django URL resolution doesn't work when running Apache/WSGI

When I run my application using Djangos built-in server, everything works ok. 当我使用Djangos内置服务器运行我的应用程序时,一切正常。 But when I try to run via Apache and WSGI the URL is no longer recognized but the it is in the urls.py file. 但是当我尝试通过Apache和WSGI运行时,URL不再被识别,但它位于urls.py文件中。

The error page I get is this: 我得到的错误页面是这样的:

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.

As you can see, the URL (app/live/) is right there in the URL patterns from the top-level urls.py file. 如您所见,URL(app / live /)就在顶级urls.py文件的URL模式中。 There are also no errors in the Apache errors.log file. Apache errors.log文件中也没有错误。

My urls.py: 我的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')),
                       )

My WSGI file: 我的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()

And finally my Apache configuration: 最后我的Apache配置:

WSGIDaemonProcess   project processes=2 maximum-requests=500 threads=1
WSGIProcessGroup    project
WSGIScriptReloading On

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

EDIT: 编辑:

After putting in some debugging output in RegexURLResolver, I saw that it tries to resolve both app/live/ and project/app/live/ . 在RegexURLResolver中输入一些调试输出后,我看到它尝试解析app/live/project/app/live/

So I changed my urls.py file to this: 所以我将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')),
                       )

Works now. 现在工作。

删除尾部斜线上WSGIScriptAlias /django工作对我来说

Hate to bring an old thread to life but, I fixed this issue by making sure my apache2 config wrapped the python path in quotes like: 讨厌带来旧线程,但是,我通过确保我的apache2配置将python路径包含在引号中来修复此问题,如:

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

I'm using Django 1.9, python3.4, and a virtualenv while following this tutorial . 我正在使用Django 1.9,python3.4和virtualenv,同时遵循本教程

UPDATE: Scratch that, I'm a maroon and didn't read well enough to see I need to use mod_wsgi in daemon mode to avoid the need to restart apache every time I make changes. 更新:从头开始,我是一个栗色的,读得不够好,看不到我需要在守护进程模式下使用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

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

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

Solution 1, edit your apache configs: 解决方案1,编辑你的apache配置:

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

Solution 2, edit your urls.py: 解决方案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')),
                   )

Also, you probably do not want to serve your static content through django if you are using apache: 此外,如果您使用的是apache,您可能不希望通过django提供静态内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM