DEV Community

Will Vincent for Learn Django

Posted on • Originally published at learndjango.com

Trailing URL Slashes in Django

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 wanted to see "Users" the full route is http://127.0.0.1:8000/admin/auth/user/ but if you instead typed 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/.

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.

[19/Mar/2020 15:49:44] "GET /admin/auth/user HTTP/1.1" 301 0
[19/Mar/2020 15:49:44] "GET /admin/auth/user/ HTTP/1.1" 200 6596
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

[19/Mar/2020 15:53:41] "GET /admin/auth/user HTTP/1.1" 404 3869
Enter fullscreen mode Exit fullscreen mode

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 suggesting the Admin example.

Top comments (0)