Search code examples
djangohttp-status-code-404urlconf

page not found without forward slash in a url django


I have the following structure for urls in my urlconf

from django.conf.urls import patterns, include, url
from django.conf import settings


urlpatterns = patterns('app.views',
                     url(r'^$', TemplateView.as_view(template_name='apps/index.html'),
                           name='index'),
                     url(r'^product/list/$','list_of_products' ,name='list_p'),
                     url(r'^product/(?P<product_id>\d+)/$','product_detail' ,name='list_detail'),
                     url(r'^order/(?P<order_id>\d+)/$','order_detail' ,name='order_detail'),
                     url(r'^order/list/$','list_of_orders' ,name='list_orders'),

)

So when i tried the above urls in the browsers like below

localhost:8000/product/list/
localhost:8000/product/4/

yup its working fine, but when i tried the above urls without forward slash at the end like below

localhost:8000/product/list
localhost:8000/product/4

I am getting page not found like below

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/product/list
Using the URLconf defined in urlconf, Django tried these URL patterns, in this 
.......
........ 

So actually why its happening, in the general scenario, even though we had not given the forward slash it should redirect to the required url right?

So can any one let me know how to avoid this, and am i doing anything wrong in defining urlconf ?


Solution

  • You must ensure that APPEND_SLASH setting is set to True in your settings.py.
    Docs: link