Django Tutorial · More Django

Add Slug Field

Learn all about Add Slug Field in this comprehensive tutorial.

5 min read intermediate
  • Have you ever seen url's that look like this:
  • If you have followed our Django Project created in this tutorial, you will have a small Django project looking like this:
  • It would have made more sense if the url looked like this:
  • Start by adding a new field in the database.
  • Now we have a new field in the database, but we also want this field to be updated automatically when we set the firstname or lastname of a member.
  • Now we can replace the ID field with the slug field throughout the project.
  • We also have to make some changes in the urls.
  • Finally, change the details view to handle incoming request as slug instead of ID:

What is Slug?

Have you ever seen url's that look like this:

The "learn-about-slug-field" part is a slug.

It is a description containing only letters, hyphens, numbers or underscores.

It is often used in url's to make them easier to read, but also to make them more search engine friendly.

Url Without Slug

If you have followed our Django Project created in this tutorial, you will have a small Django project looking like this:

And if you click the first member, you will jump to this page:

Check out the address bar:

The number "1" refers to the ID of that particular record in the database.

Makes sense to the developer, but probably not to anyone else.

Url With Slug

It would have made more sense if the url looked like this:

Check out the address bar:

That is a more user friendly url, and Django can help you create such url's in your project.

Modify the models.py File

Start by adding a new field in the database.

Open the models.py file and add a field called slug with the data type SlugField:

This is a change in the Model's structure, and therefor we have to make a migration to tell Django that it has to update the database:

python

And the migrate command:

python

Change Admin

Now we have a new field in the database, but we also want this field to be updated automatically when we set the firstname or lastname of a member.

This can be done with a built-in Django feature called prepopulated_fields where you specify the field you want to pre-populate, and a tuple with the field(s) you want to populate it with.

This is done in the admin.py file:

Enter the Admin interface and open a record for editing:

Click "SAVE" and the "slug" field will be auto populated with the firstname and the lastname, and since the "slug" field is of type SlugField, it will "slugify" the value, meaning it will put a hyphen between each word.

Next time you open the member for editing you will see the slug field with value:

Note: Note: Since the new field is empty by default, you have to do this save operation for each member.

Modify Template

Now we can replace the ID field with the slug field throughout the project.

Start with the all_members.html template, where we have a link to the details page:

Modify URL

We also have to make some changes in the urls.pyfile.

Change from <int:id> to <slug:slug>:

Modify View

Finally, change the details view to handle incoming request as slug instead of ID:

Now the link to details works with the new slugified url:

If you have followed all the steps on your own computer, you can see the result in your own browser: 127.0.0.1:8000/members/.

If the server is down, you have to start it again with the runserver command:

python

Module quiz

2 questions
1

Which of the following is true about Add Slug Field?

2

What is the most common pitfall when working with Add Slug Field?

Answer all questions to submit.