Including a Django app's url.py is resulting in a 404 (The current URL, rango/, didn't match any of)

 Question:The current URL, rango/, didn't match any of these.

我不是答案的原创者,我只是答案的搬运工。

I have the following code in the urls.py in mysite project.

/mysite/urls.py

from django.conf.urls.defaults import *
urlpatterns = patterns('',
    (r'^gallery/$', include('mysite.gallery.urls')),
)

This results in a 404 page when I try to access a url set in gallery/urls.py.

/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 error

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.
Answer:



down vote accepted

Remove the $ from the regex of main urls.py

urlpatterns = patterns('',
    (r'^gallery/', include('mysite.gallery.urls')),
)

You don't need gallery in the included Urlconf.

urlpatterns = patterns('',  
    (r'^browse/$', 'mysite.gallery.views.browse'),
    (r'^photo/$', 'mysite.gallery.views.photo'),
)

Read the django docs for more information

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.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值