Django some URLs don’t work

I’ve been making some URLs in Django, including them and so on. Everything looks fine, but no matter what i do i always end up with a 404 on some of the simplest URLs.

For example I can browse myapp/0 abd myapp/1/details/, but i get 404’d at myapp/foo

So here are my urlconf :

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^myapp/', include('myapp.urls')),
]

and myapp urlconf :

urlpatterns = [
    url(r'^foo/$ ', FooView.as_view()),
    url(r'^(?P<bar_id>d+)/$', BarByIdView.as_view()),
    url(r'^(?P<bar_id>d+)/details/$', BarDetailsByIdView.as_view()),
]

And when i tryp myapp/foo Django shows me the following urls list :

^admin/
^myapp/ ^foo/$
^myapp/ ^(?P<bar_id>d+)/$
^myapp/ ^(?P<bar_id>d+)/details/$

Answer

In the myapp.conf you have added a / at the end of the url pattern

url(r'^foo/$ ', FooView.as_view()

This must be viewed with /myapp/foo/ and not /myapp/foo because the first one matches the regex where as the second one won’t.

Leave a Reply

Your email address will not be published. Required fields are marked *