What can be easier? Populate a slug
field, make a get_absolute_url
, make a simple view
, add a line to urlpatterns
. Right? Well guess again.
Sailing the sea of back-end development I expect my core crew to be adequate. Apparently, I want too much.
Let’s look at possible reasons why something goes wrong with personal urls
for model instances. It is usually very silly, but you can get stuck for quite some time figuring out what went wrong. The list is being updated (it always is).
Overlapping urls
For example, you want your NewModel’s instances at memes/<slug>/
, but you have a memes/listing/
and one of your instance’s slug is listing
etc. Than in your urlpatterns you need to position memes/<slug>/
above anything else. This won’t let you access memes/listing/
, but will work for instance urls.
Wrong model
You may have copied another DetailView and forget to change:
class UniversalView(DetailView):
model = OldModel
to
class UniversalView(DetailView):
model = NewModel
This place is hard to even consider worth wasting time on checking. And yet.
Slug is not unique
Say, your slug is made of fields that were not originally unique. For example, name field had unique=False, some instances has same name values. Then you made a slug field and set its population to this somewhere in pre_save signal or in save() method override:
self.slug = django.utils.text.slugify(self.name)
As a result, some of your slug
s are not unique. That can be fast fixed if you add self.pk
to the mix:
self.slug = django.utils.text.slugify(self.name) + str(self.pk)