• Ctrl+C+Python
  • Posts
  • 🐍 Adding Slugs & Links to Your Django Blog Posts

🐍 Adding Slugs & Links to Your Django Blog Posts

πŸš€ Create SEO-friendly blog post URLs and make your Django site more professional in minutes!

There’s a reason 400,000 professionals read this daily.

Join The AI Report, trusted by 400,000+ professionals at Google, Microsoft, and OpenAI. Get daily insights, tools, and strategies to master practical AI skills that drive results.

πŸ“Œ NOTE: This post builds upon the Django website we created earlier. If you missed out, see this post!

πŸš€ Want a quick way to type in the python manage.py commands?

πŸ‘‰ See this post!

1. Why Use Slugs?

Slugs are a great way to create SEO-friendly URLs and make your blog posts easily accessible. Instead of using numerical IDs in your URL, a slug transforms the title into a readable and user-friendly format.

2. Updating the Post Model

Modify your Post model to include a slug field and generate slugs automatically from the title.

blog/models.py:

πŸ’‘ What’s New?

  • Added a slug field.

  • Auto-generates a slug from the title if it’s not provided.

3. Updating URLs

Now modify blog/urls.py to include a path for individual blog posts.

blog/urls.py:

4. Updating Views

Now, create a view to handle displaying individual posts.

blog/views.py:

5. Updating Templates

Blog Home (blog/blog_home.html)

Modify the blog post list to include the clickable links:

Post Detail (blog/blog_post_detail.html)

Create a new template to display the full blog post.

6. Migrate Model Changes

Run the following commands in your terminal to apply your changes:

# For experts with the Django Terminal Aliases Template:
djmakemigrations
djmigrate

# For everyone else:
python manage.py makemigrations
python manage.py migrate

πŸ† Conclusion

Tomorrow we’ll go over how to add an example blog post!

Having trouble with this step? Let me know in the comments! πŸš€πŸ

✨ And don’t forget to grab your Django Terminal Shortcuts Template below!

πŸ‘‡πŸ‘‡πŸ‘‡

Happy coding! πŸš€πŸ

Digital Shade

Want more Python tips and tricks?

πŸ”” Join my Beehiiv newsletter for exclusive cheat sheets, Python hacks, and expert tutorials!

Reply

or to participate.