Django and slugs

by mark | 26 Apr 2022, 8:51 p.m.

Updated by mark | 26 Apr 2022, 8:55 p.m.

I decided to move from using article ids to slugs in the URLs on this website. It was surprisingly straightforward. (Slugs are the bits in the URL like django-and-slugs instead of article ID numbers which are less appealing. 

  • Install the django-autoslug thing using pip and pipenv.
  • Add the field to the model. Make sure the migration touches all instances so the slug gets populated.
  • Make sure all url things use slug, not id
  • Make sure all templates call the right url

I thought it would be well hard. But not. The hardest bit was adding the following operation in a migration:

def add_slug_fields(apps, schema_editor): Article = apps.get_model('articles','Article') for instance in Article.objects.all(): instance.save()

well when i can get python to format in this. One thing at a time. 

No comments

Back to all articles