[Django]-Trailing slash "/" not added to root url

3👍

This is actually an artifact of modern browsers’ attempts to beautify HTTP. If you copy the URL from the URL bar (or view the HTTP headers), you’ll likely see that the trailing slash is indeed there.

Update

I wasn’t looking closely enough your urls.py. You just need to do this:

urlpatterns = patterns('',
    (r'^$', PictureCreateView.as_view(), {}, 'upload-new'),
    (r'^delete/(?P<pk>\d+)/$', PictureDeleteView.as_view(), {}, 'upload-delete'),
    (r'^fileupload/media/(.*)$', 'django.views.static.serve', 
    {'document_root':os.path.join(os.path.abspath(os.path.dirname(__file__)),'media')}),

)

Note the / added to the upload-delete URL (do the same for any other views you wish to end in a slash). The way APPEND_SLASHES works is better documented in the CommonMiddleware docs than it is in the settings docs. The gist of it is: if A) request.path doesn’t match any URL pattern in your application, and B) request.path + '/' does match a URL pattern in your application, Django will redirect to the latter.

Your the upload-delete URL wouldn’t have matched /delete/123/ even if you manually typed it in manually, since the extra / wouldn’t match the regexp.

Leave a comment