[Fixed]-Append Slash not working

19👍

APPEND_SLASH doesn’t happen unconditionally — it only comes into effect if, after trying all existing URL patterns (and the associated view, if one matches), Django is about to return a 404.

If that’s the case, and the original request didn’t have a slash at the end, then Django checks to see whether any URL patterns would match with a trailing slash. If so, it issues an HTTP redirect.

If any of your URL patterns match the original request (without the slash), then Django will try that one first. If that raises an exception, then you will see it (I suspect that this is what is happening). Django won’t ever get to issue the redirect.

4👍

If you have added some of your own middleware, the order of the middleware is important. I had a similar case where the slash was working before and adding a middleware item broke it. After switching the order, everything started working again.

3👍

This will surely fix your problem. You can give the url with or without a trailing slash, it will give you the same required result.

In the urls file,

urlpatterns = patterns('', url(r'^allvideo/?$','my.views.allvideo'))

Add question mark after the trailing slash.
When you enter an url it will first check with the trailing slash or it would match it without the trailing slash and in either case will give you the same response. Hope this was of some help to you. Happy Coding.

1👍

It might be caused by one of your apps having a slug on it’s own. Django will try to match the non-trailing slash version to the slug. For example:

In your root url.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path("register/", include("user_register.urls")),
    path('',include("django.contrib.auth.urls")),
    path('', include("app.urls")),]

In your app.urls:

urlpatterns = [
    path("", views.Index.as_view(), name="index"),
    path("<slug:slug>", views.AppView.as_view(), name="app"),]

In this case http://mysite/register will not redirect because Django first looks for the slug in app.urls to fix it you have to prefix the slug with something. For example:

urlpatterns = [
    path("", views.Index.as_view(), name="index"),
    path("app/<slug:slug>", views.AppView.as_view(), name="app"),]

0👍

Not sure what is the root cause, but these might help you to go around it or narrow it down:

(1) Have you tried either using the actual view function as an argument (instead of a string):

from my.views import allvideo
urlpatterns = patterns('', url(r'^allvideo/$',allvideo))

(2) Or skip using the url-function:

urlpatterns = patterns('', (r'^allvideo/$','my.views.allvideo'))
👤Lycha

-1👍

Even if it seems unbelievable, I had the same issue and just restarting the development server fixed it. Just to remember:

python manage.py runserver

Leave a comment