本文共 1362 字,大约阅读时间需要 4 分钟。
DJANGO的TAG分为三类:
• simple_tag : Processes the data and returns a string
• inclusion_tag : Processes the data and returns a rendered template• assignment_tag : Processes the data and sets a variable in the context
blog_tags.py
from django import templateregister = template.Library()from ..models import Postfrom django.db.models import Count@register.simple_tagdef total_posts(): return Post.published.count()@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}@register.assignment_tagdef get_most_commented_posts(count=5): return Post.published.annotate(total_comments=Count('comments')).order_by('-total_comments')[:count]
latest_posts.html
base.html
无css层展示
转载地址:http://dwrxa.baihongyu.com/