How to Trailing URL Slashes in Django?

Published On: 21/10/2022 | Category: Django

Hi Dev,

Here, I will show you how to works how to trailing url slashes in django. We will use django append slash. We will look at example of trailing slash in url django. you will learn how to remove trailing slash from url. follow bellow step for django trailing slash redirect.

One of the numerous built-in features of Django is APPEND SLASH, which by default is set to True and adds a slash (/) to URLs that would otherwise return an error.

When you type 127.0.0.1:8000/admin/auth/user into your web browser, it immediately redirects to http://127.0.0.1:8000/admin/auth/user/. For instance, in the Admin of any Django project, the entire route to view "Users" is http://127.0.0.1:8000/admin/auth/user/.

And if you look at the logs in your command line, you'll see something like the following which shows a 301 redirect was perfored my Django to append the slash.

[21/Oct/2022 15:49:44] "GET /admin/auth/user HTTP/1.1" 301 0
[21/Oct/2022 15:49:44] "GET /admin/auth/user/ HTTP/1.1" 200 6596

Let's disable the default setting right away to make sure Django is handling this for us. Add the following line at the bottom of your settings.py file:

setting.py
APPEND_SLASH = False

To incorporate the modifications, the local Django webserver will immediately restart. Refresh the page at 127.0.0.1:8000/admin/auth/user without the trailing slash and give it another try.

The result? A 404 page! Because we've turned APPEND_SLASH off.


You can see this in the logs as well.

[21/Oct/2022 15:49:44] "GET /admin/auth/user HTTP/1.1" 404 3869

I Hope It will help you....