Django自定义包含标签

时间:2020-04-14 20:52:29

标签: python django

我正在尝试使用自定义包含标记在我的博客侧边栏上显示最新的3个帖子标题,我似乎没有出现任何错误,但我的结果也未显示。

templatetags.py

(SELECT PERCENTILE_DISC(reps, .5) OVER() FROM UNNEST(arr_tab.arr) AS reps LIMIT 1) AS med2

Inclusion tag in latest_posts.html file Latest_post.html

from django import template
from ..models import Post

register = template.Library()

@register.inclusion_tag('blog/post/latest_posts.html')
def show_latest_posts(count=5):
    latest_posts = Post.published.order_by('-publish')[:count]
    return {'latest_posts': latest_posts}

templatestag.py

base.html

<ul>
    {% for post in posts %}
        <li>
            <a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
        </li>
    {% endfor %}
</ul>

including the template in base.html

这是我的文件结构

{% load blog_tags %}
{% load static %}
<h3>Latest Posts</h3>
{% show_latest_posts 3 %}

1 个答案:

答案 0 :(得分:0)

看来您的模板是错误的。只需将帖子更改为latest_posts

<ul>
    {% for post in latest_posts %}
        <li>
            <a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
        </li>
    {% endfor %}
</ul>