Slug Based Routing In Django

Slug Based Routing In Django

Our Post model has a slug field that we had set up earlier. Now we want to configure slug based routing in Django. To design URLs for an app, we learned how to use the Python module URLconf or URL configuration. The URLconfg uses Python code to map between URL path expressions and Python functions that are defined in views. Some routing paths are static, and some are dynamic. In other words, we can use variable data in the URL to determine how to map to a particular view function. We’ve seen how to use integer-based ID routing and in this tutorial, we’ll see how to instead use string-based slugs.


Slug Definition

The Post mdel has the slug defined using slug = models.SlugField() which is highlighted below. Our system currently has three Post models in the database and each one has a slug associated with it. Our slugs so far are a-new-title, second-post, and third-post.
posts/models.py


Defining URL Path For Slug Capture

Web applications respond to Http requests and use parameters in the request to determine how to route. Our goal is to be able to visit links like http://localhost:8000/posts/a-new-title/, http://localhost:8000/posts/second-post/, and http://localhost:8000/posts/third-post/ and Django will route to each particular Post detail view. To capture a slug variable in the URL, we can use the <slug:slug> patten in the path() function. This is highlighted in the code below.

posts/urls.py

The other important thing to note in the code above is that the name parameter is set to post_detail. This is a named URL pattern, or a named route. This gives you the ability to link to names rather than hardcoding in hrefs. We’ll see how that works in a bit.


Using The Captured Slug

Now in the post_detail() function in the views.py file, we can accept the captured slug from the URL using post_detail(request, slug). From there, we then use the Django ORM to query the database using that slug with the Post.objects.get(slug=slug) code. Lastly we render the post_detail.html template while passing the context variable which holds the result of the query to the database.

posts/views.py


Linking Via Slug

The post list page needs each post to link via slug to the post detail page. We specify this in the href attribute using {% url ‘post_detail’ post.slug %}. That code is highlighted here.

posts/templates/posts/post_list.html

Now we have links using a slug as we see here.

django link via slug


Using a Post Detail Template

All the plumbing is in place, now all we have to do is provide a post_detail.html template like so.

posts/templates/posts/post_detail.html

slug example one

djanglo slug two

django slug three

Learn More

Slug Based Routing In Django Summary

In order to make URLs pretty in Django, we can use slug based routing. Using slugs also provides SEO benefits as search engines will understand a descriptive slug better than a simple ID in the URL. In this tutorial we learned how to handle using a slug for routing, querying the database, and rendering a template for a post detail page.