python - 包含 Django 应用程序的 url.py 会导致 404

标签 python django url-routing http-status-code-404 django-urls

我在 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 encounters include(), 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/

相关文章:

python - 如何在 sympy + IPython 中禁用 init_printing

python - 如何使用 django 在网络浏览器上推送通知

Django "login() takes exactly 1 argument (2 given)"错误

python - 为什么 Django 的 Meta 是一个老式的类?

javascript - 以 SEO 友好的方式使用 Javascript 将 URL 路由映射到操作

javascript - html5模式下的AngularJS路由错误

python - 创建一个计数器,迭代数据帧中的列,并在满足列中的条件时进行计数

python - 根据列值拆分数据框并导出到不同的 Excel 工作表

python - 我可以同时使用 uwsgi + (tornado, gevent, etc) 吗?

Symfony2 : Routing controllers and views in subdirectories