Trailing URL Slashes in Django

Updated

Among Django's many built-in features is APPEND_SLASH, which by default is set to True and automatically appends a slash / to URLs that would otherwise 404.

Note: The Chrome web browser automatically appends a trailing slash / to URLs so you'll need to use something like Safari to see this in action.

For example, within the Admin of any Django project, if you want to see "Users," the full route is http://127.0.0.1:8000/admin/auth/user/. However, if you type into your web browser 127.0.0.1:8000/admin/auth/user (notice no / at the end there!), it automatically redirects to http://127.0.0.1:8000/admin/auth/user/.

If you look at the logs in your command line, you'll see something like the following: Django performed a 301 redirect to append the slash.

[16/Jul/2024 15:49:44] "GET /admin/auth/user HTTP/1.1" 301 0
[16/Jul/2024 15:49:44] "GET /admin/auth/user/ HTTP/1.1" 200 6596

Now, let's turn off the default setting to confirm that Django is performing this action for us. Within your settings.py file, add the following line at the bottom:

# settings.py
APPEND_SLASH = False

The local Django webserver will automatically restart to reflect the changes. Try again to refresh the web page for the User section of the admin without the trailing slash: 127.0.0.1:8000/admin/auth/user.

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

404 Page

You can see this in the logs as well.

[16/Jul/2024 15:53:41] "GET /admin/auth/user HTTP/1.1" 404 3869

Therefore as a practical matter make sure that you include a / at the end of your Django URLs. You don't have to but absent this nice built-in feature--or Chrome--you'll receive unexpected errors.

Special thanks to Carlton Gibson for help on this topic and for suggesting the Admin example.