1👍
✅
You need to add a slug
field to your model. Refer example below :
class Comment(models.ModelField):
.... # all other fields
slug = models.SlugField()
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(Comment, self).save(*args, **kwargs)
Then your get_absolute_url
becomes
def get_absolute_url(self):
return reverse('comment_thread', kwargs={"id" : self.id, "slug" : self.slug})
The save
function defined above basically sets the slug of the model the first time its saved (using its name as the url). This prevents the url of your comment from changing even if you edit it later.
Even for Post
change the slug
field to models.SlugField()
Source:stackexchange.com