我在 mysite 项目的 urls.py 中有以下代码。
/mysite/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^gallery/$', include('mysite.gallery.urls')),
)
当我尝试访问 gallery/urls.py 中设置的 url 时,这会导致出现 404 页面。
/mysite/gallery/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^gallery/browse/$', 'mysite.gallery.views.browse'),
(r'^gallery/photo/$', 'mysite.gallery.views.photo'),
)
404错误
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^gallery/$
The current URL, gallery/browse/, didn't match any of these.
此外,该站点托管在媒体神殿 (dv) 服务器上并使用 mod_wsgi
最佳答案
从 urls.py
的正则表达式中删除 $
urlpatterns = patterns('',
(r'^gallery/', include('mysite.gallery.urls')),
)
您不需要包含的 Urlconf 中的 gallery
。
urlpatterns = patterns('',
(r'^browse/$', 'mysite.gallery.views.browse'),
(r'^photo/$', 'mysite.gallery.views.photo'),
)
阅读django docs了解更多信息
Note that the regular expressions in this example don't have a
$
(end-of-string match character) but do include a trailing slash. Whenever Django encountersinclude()
, it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
关于python - 包含 Django 应用程序的 url.py 会导致 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2910714/