【问题标题】:Django URL resolution doesn't work when running Apache/WSGI运行 Apache/WSGI 时 Django URL 解析不起作用
【发布时间】: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

标签: python django apache wsgi


【解决方案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。

更新: 暂且不说,我是个栗色的人,没有读到足够好的内容,我需要在守护进程模式下使用 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

【讨论】:

    【解决方案2】:

    解决方案 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】:

      删除WSGIScriptAlias /django 上的斜杠对我有用

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 根据我的经验,在 WSGIScriptAlias 的 URL 路径中添加斜杠会破坏整个应用程序(404 错误,在 Debian 上使用 Apache 2)。您说这应该有效的任何理由(或参考)?
        • 在 Apaches 的 error.log 中添加一个斜杠会给我以下错误:Target WSGI script not found or unable to stat: /home/user/project/apache/project.wsgiapp
        • 这正是我的意思。如果别名以斜杠结尾(没有任何意义,但就是这样),mod_wsgi 会将别名相对 URL 附加到 WSGI 文件名。所以这并不能解决 OP 的问题。
        • AndiDog 是正确的。如果最后一个参数指的是目录而不是 WSGI 脚本文件,则尾部斜杠通常只会在 WSGIScriptAlias 上使用。在最终参数是目录的情况下,目录路径还应该有一个斜杠。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-07
        • 2013-01-02
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 2015-07-07
        • 1970-01-01
        相关资源
        最近更新 更多