[Fixed]-Bad Regex in Django? DetailView, ListView, and blog construction

1👍

Ideally, you should use reverse in get_absolute_url, instead of hardcoding it.

from django.core.urlresolvers import reverse

class Post(models.Model):
    ...
    def get_absolute_url(self):
        return reverse('post-detail', args=[self.slug])

If you do hardcode the URL, it should contain a leading slash.

    def get_absolute_url(self):
        return "/%s/" % (self.slug)

If first_post.get_absolute_url is returning the homepage url with your current get_absolute_url, that suggests that the slug is an empty string.

Leave a comment