As of today, each post's url in the address bar is a slug such as 'adding-slugified-urls' instead of the post id...e.g.

post/15

This improves SEO (and makes the share links a lot more comprehensible). This involved modifying A LOT of files and broke tons of stuff before I fixed it. Here is an example of how my urls.py changed:

Code changes needed to slugify posts. The left side of the image shows the before and the right side shows the after.

After adding a new slug field to the post model, you add it to the post form as a text field. This allows you  to set the slug to whatever you want.

Create post view showing slug field

Because the url changes, you need to update all of your templates so the hrefs to posts reference the post.slug instead of post.id.

Finally, an update to views.py file ensures a db lookup of a post used the slug for a unique lookup instead of the post id.

post = Post.objects.get(slug=self.object.slug)

Everything seems works so far!

Back to Home
 Profile Picture
Profile Picture

About John Solly

I am a Senior Software Engineer with a focus on geospatial applications, based in the Columbus, OH metropolitan area. This blog is where I delve into the intricacies of GIS (Geographic Information Systems), offering deep dives into different components of the geospatial technology stack. For those who share a passion for GIS and its applications, you've found a spot to explore and learn.

Interested in collaborating or learning more about my work? Take a look at my portfolio for a showcase of my projects and expertise.

Comments

  • No comments yet.
Login to Comment