0

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 ?

4

1 回答 1

0

您必须确保APPEND_SLASH在 settings.py 中将设置设置为 True。
文档:链接

于 2013-11-30T10:22:02.773 回答