- 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

Reply