鍍金池/ 問答/Python/ Django2.0.1模板中的get_absolute_url不顯示

Django2.0.1模板中的get_absolute_url不顯示

Django2.0.1模板中在a標簽使用get_absolute_url,網(wǎng)頁上不顯示,也沒有報錯。
模板代碼:

{% for c in categories %}
        <li {% if category.slug == c.slug %}class="selected"{% endif %}>
            <a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
        </li>
 {% endfor %}

{{ c.get_absolute_url }}

換成

{% url 'shop:product_list_by_category' c.slug %}

后就能正常顯示,請問說什么原因?qū)е碌哪?/p>

models代碼:

class Category(models.Model):
    name = models.CharField(max_length=200,
                        db_index=True)
    slug = models.SlugField(max_length=200,
                        db_index=True,
                        unique=True)

    class Meta:
        ordering = ('name',)
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.name

    def get_absolute_rul(self):
        return reverse('shop:product_list_by_category',
                   args=[self.slug])
                   

urls代碼:

    url(r'^(?P<category_slug>[-\w]+)/$', views.product_list,
    name='product_list_by_category')

views代碼:

    def product_list(request, category_slug=None):
        category = None
        categories = Category.objects.all()
        products = Product.objects.filter(available=True)
        if category_slug:
            category = get_object_or_404(Category, slug=category_slug)
            products = products.filter(category=category)
        return render(request,
              'shop/product/list.html',
              {'category': category,
               'categories': categories,
               'products': products})
回答
編輯回答
念舊

get_absolute_url

2017年3月9日 14:44