Someone inputs www.example.com/3/
I have a model with a charfield that I can slugify.
class Mymodel(models.Model):
title = models.CharField(max_length=200)
How can I rewrite the url as www.example.com/3/title-of-selected-id in Django?
Do I do it in the urls.py or in the views.py? All I could think of was making 2 patterns like this
(r'^mymodels/(?P<mymodel_id>\d+)/(?P<name_slug>[\w-]+)$', 'app.views.detail'),
(r'^mymodels/(?P<mymodel_id>\d+)/$', 'app.views.rewrite'),
And then in my app.views.rewrite I would get the mymodel title, slugify it and redirect to the first url. But this seems awfully dirty
Through #django irc someone told me that the redirect method wasn’t bad, and we couldn’t think of another easy way so I just coded something up which seems to be working.