How to Create a SlugField in Django


In this article, we show how to create a SlugField in Django.

A SlugField is a field that stores the slugified version of a string.

For example, say a post is entitled, "How to shave your beard". The slugified version of this post is, "How-to-shave-your-beard"

Slugs are created mostly for the purpose of creating a nice, clean URL.

Say for example, you have a site of user-generated posts, such as stackoverflow or quora.

A user starts a post that has a title.

Each post creates a separate web page based on the title.

Now if a user asks the question, "How do you slugify text in Python"

If a URL is created for this question, as is, with the spaces in them, the browser will insert %20 in place of each space. Therefore, the URL will be, How%20do%20you%20slugify%20text%20in%20Python

This will work, but it looks extremely ugly and isn't very human readable.

So instead of having spaces in a URL, a slugified text is created that contains no spaces. Instead of spaces there are "-" instead. Therefore, the URL will be, How-do-you-slugify-text-in-Python

This looks much cleaner and is much more human readable.

So now you see the importantce of slugifying text if you are going to generate URLs based off of a particular post.

So let's now generate a SlugField in Django.

The code is shown below.

We create a database table called UserPost and have the fields, title, date_published, and url. The url field will have the slugified version of the title.

So to create a database table we have to import models from django.db

We then import slugify from django.template.defaultfilters

This is because we want to slugify the slugfield which we create, url, which is the field that contains the slug of the title field.

The first field of this UserPost database table is, title. This is a CharField that has a maximum length of 500 characters.

We then put the date in which the post was created.

We then create the field, url, which is a SlugField.

Now the approach that we take and the approach that you should take is to not have to manually enter anything into the SlugField, url. Instead, the slug should be automatically created based off of the title that the user enters for the post. Of course if you want to manually insert the slug (not sure why you would want to), you can just put the line, url= models.SlugField(max_length=500, unique=True) and delete the save() method. But you, for the vast majority of the time, if not always, want the slug generated automatically based off of another field of your database table or form.

So what you have to do is enter in the statement, blank=True, into the SlugField. What this does is it allows the SlugField to be entered in with nothing entered into it. We then create a function that enters the slugified title field into the SlugField.

You want the SlugField to have the line, unique=True, because you can't have multiple pages linked to the same URL.

We then create a save function that overrides data that the user has entered.

In this save() function, we reference the SlugField as, self.url

We set, self.url equal to, slugify(self.title)

We then save the changes with the line, super(UserPost, self).save(*args, **kwargs)

So now once you have saved the title field, the slugified version of the title will appear in the SlugField, url.

This is shown in the output below.

Django SlugField with slug generated automatically

So you can see any title that you enter in will have its slug automatically generated in the SlugField.

The above code is great and it works.

However, if you're creating URLs based off of user-generated input, you generally don't want the URL to be editable. Therefore, it all depends on you as the programmer, but to make sure that the SlugField can't be changed, you should add in the statement, editable=False, into the SlugField. You may want to make the title uneditable as well. This is up to you. Now with this code, the SlugField won't change if the title is changed. This way, the URL is stable even if the title changes.

And this is how you can create a SlugField in Django in which the slug is automatically generated.

Related Resources

How to Create a Video Uploader with Python in Django

How to Create an Image Uploader with Python in Django

HTML Comment Box is loading comments...