What is a slug in Django?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Django, a slug is a short, URL-friendly string used to identify an object in a readable way. It is usually derived from a title or name, such as turning Django Slug Tutorial into django-slug-tutorial. Slugs are useful because they make URLs easier for humans to read and often more meaningful than using an opaque numeric ID alone.
What Makes a String a Slug
A slug is typically:
- lowercase
- made of letters, numbers, hyphens, or underscores
- safe to place in a URL path
- stable enough to identify a specific object
Django provides SlugField for storing such values and slugify for generating them.
A simple example of slugify:
This produces values like django-slug-tutorial and hello-world.
Using SlugField in a Model
A common pattern is to store the slug directly on the model.
Now each Article can be addressed by a readable URL component instead of only by numeric ID.
Using Slugs in URLs
Django's URL routing includes a slug path converter.
And the corresponding view might be:
That produces readable URLs such as /articles/django-slug-tutorial/.
Why Slugs Are Useful
Slugs improve:
- URL readability
- memorability and sharing
- semantic meaning in links
- search-engine friendliness in many cases
They are especially common for blog posts, products, categories, and public pages where the URL should communicate something about the content.
Uniqueness and Stability
Two design questions matter immediately:
- must the slug be unique across the table
- should the slug change when the title changes
Many applications set unique=True and keep the slug stable once published. That avoids broken links.
If you want automatic uniqueness, you may need to append a suffix when the base slug already exists.
This creates my-title, then my-title-1, and so on.
Slug Versus ID
Some teams use only slugs in public URLs. Others combine IDs and slugs for stability and efficiency.
For example:
In that pattern, the ID is the true lookup key and the slug is primarily for readability. That can be a good compromise when titles may change over time.
Common Pitfalls
The most common mistake is regenerating the slug every time the title changes. That can break existing links unless you manage redirects deliberately.
Another mistake is assuming slugify alone guarantees uniqueness. It only normalizes the string; it does not prevent collisions between similar titles.
Developers also sometimes use slugs for private or internal resources where readability does not matter and numeric or UUID keys would be clearer.
Finally, be careful with international text and transliteration expectations. slugify is useful, but multilingual SEO and URL rules may require more deliberate handling.
Summary
- A Django slug is a URL-friendly identifier, usually derived from a title or name.
- Use
SlugFieldto store it andslugifyto generate it. - Slugs make URLs more readable and meaningful than raw IDs.
- Decide early whether slugs must be unique and whether they should stay stable over time.
- For mutable titles, consider combining an ID with a slug or managing redirects explicitly.

