What is a "Slug" in Django?
Claudio Russi
May 4, 2020
The word slug comes from the news media. In that industry, a slug is an informal way of naming stories during the production process.1 It's a way to describe the content of the article by using keywords and concise information.
The concept is similar in web development. A slug is the part of a URL that identifies a page on a website in a user-friendly way, using keywords to give an idea about the content of the page.2 We can take the URL of another of our posts as an example: https://sophilabs.co/blog/machine-learning-for-sentiment-analysis. The slug is machine-learning-for-sentiment-analysis, and it clearly states what the post is about.
Why You Should Use Slugs
Most search engines use web page URLs alongside the title, metadata, and the content itself to detect where your page should rank on the search results. Google recommends using hyphens (-) to separate words, since they treat them as whitespaces. Also, you should keep URLs short and concise so every word has more value on the engine’s web crawler. Adding too many similar words can be considered keyword stuffing, and your site could be penalized or banned by some search engines.3
Slugify Your URL Manually
Taking as example the machine learning post we mentioned previously, we could use its title (How to Use Machine Learning for Sentiment Analysis with Word Embeddings) to generate a URL that indicates its content and gives information to the user. As URLs can’t include spaces, we need to replace them with %20
(it’s value in URL encoding), which make the URL look like this:
How%20%to%20%Use%20Machine%20Learning%20for%20Sentiment%20Analysis%20with%20Word%20Embeddings
Not clear or user friendly at all, but if we instead slugify the URL, replacing the spaces with hyphens and downcasing all letters, it would look like this:
how-to-use-machine-learning-for-sentiment-analysis-with-word-embeddings
It is clearer, but is still too long, and it contains words that are not important and don't add meaning to the URL. We should filter the title before slugifying it to only show the important words.
machine-learning-for-sentiment-analysis
As you can see, it’s a lot better than the first example. It is user-friendly, it’s short and concise, and it clearly states what the post is about.
But what if the titles of the blog posts on our site are not unique? Wouldn’t we get a repeat URL? In that case, you can add the Id of the post before the slug, making it unique to every page.
123/machine-learning-for-sentiment-analysis
Slugs in Django
Django comes with some utilities that help us to easily add slugs to our app.
SlugField
If we have a post model and want to add a field to the database to store the slug, we can use SlugField, which works similarly to the CharField but will validate that the information we store is in slug format and doesn’t contain invalid characters.
Slugify
Django also includes a function on the utils.text module named slugify which converts a normal string into a URL slug by
-
removing characters that aren’t alphanumerics, underscores, hyphens, or whitespaces
-
removing leading and trailing whitespaces
-
converting to lowercase
-
replacing any whitespace or repeated dashes with single dashes.
Slugify expects as parameters the string we want to slugify and an optional boolean specifying if we want to allow unicode characters in the slug.4
>>> slugify(' Machine Learning for Sentiment Analysis ')
'machine-learning-for-sentiment-analysis'
Now that we've demonstrated what slugs are and how they work in Django, we hope you can get the most out of them!
-
Wikipedia, s.v. "Slug (publishing)" last updated May 6, 2019. ↩
-
"What is a slug and how to optimize it?" Yoast, September 18, 2019. ↩
-
"SEO Friendly URLs" Backlinko, accessed 27 April 2020. ↩
-
"Django Utils," Documentation, Django Project, accessed April 27, 2020. ↩
Combining QuerySets in Django
In this article we'll demonstrate several approaches to merging QuerySets either with the same or different models in Django without relying on raw queries.
Complex Outer Joins with Django’s FilteredRelations
Today we will look at a problem that is trivial to solve with plain SQL but, until recently, couldn't be solved with the ORM without resorting to more complex approaches.
Photo by Wolfgang Hasselmann.
Categorized under research & learning.We are Sophilabs
A software design and development agency that helps companies build and grow products by delivering high-quality software through agile practices and perfectionist teams.