DEV Community

Will Vincent for Learn Django

Posted on • Originally published at learndjango.com

12 1

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay