Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django URL解析不';运行Apache/WSGI时无法工作_Python_Django_Apache_Wsgi - Fatal编程技术网

Python Django URL解析不';运行Apache/WSGI时无法工作

Python Django URL解析不';运行Apache/WSGI时无法工作,python,django,apache,wsgi,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 patte

当我使用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.
最后是我的Apache配置:

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

WSGIScriptAlias /project /home/user/project/apache/project.wsgi
WSGIScriptAlias /project/ /home/user/project/apache/project.wsgi
编辑:

在RegexURLResolver中输入一些调试输出后,我看到它尝试解析
app/live/
project/app/live/

因此,我将URL.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')),
                       )

现在可以工作了。

在Apache配置中向URL路径前缀添加尾随斜杠:

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

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

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

WSGIScriptAlias / /home/user/project/apache/project.wsgi
解决方案2,编辑URL.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')),
                   )
从django.conf.url.defaults导入*
从django.conf导入设置
urlpatterns=模式(“”,
(r'^/project/media/(?P.*)$,'django.views.static.service',
{'document_root':settings.MEDIA_root}),
(r'^/project/app/live/',包括('project.app.url',app_name='live'),
)
此外,如果您使用的是apache,您可能不希望通过django提供静态内容:

Alias /media /path/to/media/root
<Location /media>
SetHandle None
</Location>
Alias/media/path/to/media/root
设置句柄无

删除
WSGIScriptAlias/django
上的尾随斜杠对我来说很有用

我讨厌让一个旧线程复活,但我通过确保我的apache2配置将python路径用引号括起来解决了这个问题,如:

WSGIScriptAlias / /home/user/django_project/wsgi.py
WSGIPythonPath "/home/user:/home/user/.virtualenvs/djangodev/lib/python3.4/site-packages"
我正在使用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

根据我的经验,在WSGIScriptAlias的URL路径中添加斜杠会破坏整个应用程序(404个错误,在Debian上使用Apache2)。有什么理由(或参考)说这应该有效吗?在Apaches error.log中添加一个尾随斜杠会给我以下错误:
Target WSGI script找不到或无法统计:/home/user/project/apache/project.wsgiapp
。这正是我的意思。如果别名以斜杠结尾,mod_wsgi会将别名相对URL附加到wsgi文件名(没有任何意义,但就是这样)。所以这并不能解决OP的问题。AndiDog是正确的。如果最后一个参数引用的是目录而不是WSGI脚本文件,则通常只在WSGIScriptAlias上使用尾随斜杠。在最后一个参数是目录的情况下,目录路径也应该有一个尾随斜杠。2)否则它找不到设置模块<代码>导入错误:无法导入设置“project.settings”(是否在sys.path上?是否有语法错误?):没有名为project.settings的模块。也许我的整个项目/应用程序设置都配置错误了?哦,我明白了,你有一个额外的“apache”子目录用于WSGI文件。nvmI没有发现您的配置有任何问题。你们有最新的mod_wsgi/Django版本吗?您还可以尝试打印debug
RegexURLResolver.resolve