G
G
Gashington2013-12-24 17:15:56
Django
Gashington, 2013-12-24 17:15:56

Why is get_absolute_url not output in template?

Hello. There was a problem rendering get_absolute_url in template.
Here is my models.py:

class Category(models.Model):
    category_name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.category_name


class Post(models.Model):
    title = models.CharField(max_length=255)
    datetime = models.DateField(u'Дата публикации')
    content = RichTextField()
    category = models.ForeignKey(Category)
    slug = models.SlugField(max_length=255, blank=True)

    def save(self, *args, **kwargs):
        if not self.id:
            self.slug = translit.slugify(self.title)
            super(Post, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return '/blog/%i/' % self.id

The link in the template looks like this
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>

Link working view <a href="/blog/1/">Первый пост</a>
But when using
def get_absolute_url(self):
        return '/blog/%i/' % self.slug)

Displays an empty link Rummaged through a bunch of forums and information But did not understand how to correctly set get_absolute_url

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maxaon, 2013-12-24
@Gashington

You have the wrong formatter (%i)

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

But the style is outdated. formatpreferred.
def get_absolute_url(self):
        return '/blog/{}/'.format(self.slug)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question