In this tutorial, we will learn how to slugify urls in Django.
A Slug is a short label for something, containing only letters, underscores or hyphens.
Slugs are generally used in URL, For example in a typical blog entry URL:
https://www.kodnito.com/posts/slugify-urls-in-django
In the url, the slugify-urls-in-django is the slug.
Sample Project
$ mkdir django_slugify && cd django_slugify
$ pipenv install django
$ pipenv shell
$ django-admin startproject config .
$ python manage.py startapp posts
from django.db import models
from django.utils.text import slugify
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Post, self).save(*args, **kwargs)
import string
import random
from django.db import models
from django.utils.text import slugify
def rand_slug():
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(6))
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(rand_slug() + "-" + self.title)
super(Post, self).save(*args, **kwargs)
INSTALLED_APPS = [
...
'posts.apps.PostsConfig',
]
Run the following commands to create the new post table.
$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py shell
>>> from posts.models import Post
>>> post = Post.objects.create(title='Slugify Urls in Django')
>>> post.slug
'slugify-urls-in-django'
urlpatterns = [
path('posts/<slug:slug>/', PostDetailView.as_view(), name='post_detail'),
]
Share this: