Django自定义模版标签

Django提供了以下函数,可以轻松地创建自己的模板标签:

  • simple_tag:处理数据并返回字符串
  • inclusion_tag:处理数据并返回呈现的模板

模板标签必须存在于Django应用程序中。

在博客应用程序目录中,创建一个新目录,将其命名为templatetags,并向其中添加一个空的__init__.py文件。在同一文件夹中创建另一个文件,并将其命名为blog_tags.py。命名文件的方式很重要。您将使用此模块的名称来加载模板中的标记。

首先创建一个简单的标记来检索博客中发布的文章总数。

编辑刚刚创建的blog_tags.py文件

python 复制代码
from django import template
from ..models import Post


register = template.Library()

@register.simple_tag
def total_posts():
    return Post.published.count()
  • total_posts()是一个简单的模板标记,它返回到目前为止发布的文章的数量。
  • 使用@register.simple_tag 装饰器将函数注册为简单标记。
  • Django将使用函数名作为标签名。
  • 如果希望使用不同的名称注册它,可以通过指定name属性来实现,比如@register.simple_tag(name='my_tag')。

📌添加新的模板标记模块后,您将需要重新启动Django开发服务器,以便在模板中使用新的标签和过滤器。

在使用自定义模板标签之前,必须使用{% load %}标签使它们在模板中可用。

打开blog/templates/base.html模板,在顶部添加{% load blog_tags %}来加载你的模板标签模块。然后,使用创建的标记来显示您的帖子总数。只需将{% total_posts %}添加到模板中。

html 复制代码
{% load blog_tags %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}{% endblock %}</title>
    <link href="{% static 'css/blog.css' %}" rel="stylesheet">
</head>
<body>
    <div id="content">
        {% block content %}
        {% endblock %}
    </div>
    <div id="sidebar">
        <h2>My blog</h2>
            <p>This is my blog.I've written {% total_posts %} posts so far.</p>
    </div>
</body>
</html>

现在创建另一个标记,在博客的侧边栏中显示最新的文章。

使用包含(模版)标记。使用包含标记,可以用模板标记返回的上下文变量呈现模板。
编辑blog_tags.py文件并添加以下代码:

python 复制代码
@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}

使用blog/post/latest_posts.html指定必须用返回值呈现的模板。

模板标记将接受一个可选的count参数,默认值为5。该参数允许指定要显示的帖子数量。

📌该函数返回一个变量字典,而不是一个简单的值。包含标签必须返回一个值的字典。

现在,在templates/blog/post/下创建一个新的模板文件,并将其命名为latest_posts.html。

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

使用模板标记返回的latest_posts变量显示了一个无序的帖子列表。

编辑blog/base.html模板

html 复制代码
    <div id="sidebar">
        <h2>My blog</h2>
        <p>This is my blog.I've written {% total_posts %} posts so far.</p>
        <h3>Latest posts</h3>
        {% show_latest_posts 3 %}

    </div>

并添加新的模板标记以显示最后三篇文章。

最后,创建一个标签来显示评论最多的帖子,它将结果存储在一个可以重用的变量中,而不是直接输出结果。

编辑blog_tags.py文件

python 复制代码
from django import template
from ..models import Post
from django.db.models import Count

register = template.Library()

@register.simple_tag
def 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.simple_tag
def get_most_commented_posts(count=5):
    return Post.published.annotate(
        total_comments=Count('comments')
    ).order_by('-total_comments')[:count]
  • 使用annotate()函数构建QuerySet来聚合每篇文章的评论总数。
  • 使用Count聚合函数在计算字段total_comments中存储每条评论的数量
  • 按计算字段降序排列QuerySet。我们还提供了一个可选的count变量来限制返回的对象总数。

编辑blog/base.html模板

html 复制代码
{% load blog_tags %}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}{% endblock %}</title>
    <link href="{% static 'css/blog.css' %}" rel="stylesheet">
</head>
<body>
    <div id="content">
        {% block content %}
        {% endblock %}
    </div>
    <div id="sidebar">
        <h2>My blog</h2>
        <p>This is my blog.I've written {% total_posts %} posts so far.</p>
        <h3>Latest posts</h3>
        {% show_latest_posts 3 %}
        <h3>Most commented posts</h3>
        {% get_most_commented_posts as most_commented_posts %}
        <ul>
            {% for post in most_commented_posts %}
            <li>
                <a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
            </li>
            {% endfor %}
        </ul>
    </div>
</body>
</html>
  • 使用{% get_most_commentted_posts as most_commentted_posts %}将模板标记的结果存储在一个名为most_commentted_posts的新变量中。
  • 然后,使用无序列表显示返回的帖子。

关于Django模版的更多信息模板 | Django 文档 | Django (djangoproject.com)

相关推荐
AI_Claude_code5 小时前
ZLibrary访问困境方案六:自建RSS/Calibre内容同步服务器的完整指南
运维·服务器·网络·爬虫·python·tcp/ip·http
weixin_462022355 小时前
Dancing under the stars: video denoising in starlight
python·计算机视觉
kishu_iOS&AI5 小时前
机器学习 —— 线性回归(2)
人工智能·python·算法·机器学习·线性回归
网上邻居YY5 小时前
深度学习DL 之 安装PyTorch·GPU版、CUDA(本人Anaconda、Python、PyCharm已提前安装好)
pytorch·经验分享·python·深度学习·pycharm·学习方法
AI、少年郎6 小时前
如何用个人电脑快速训练自己的语言模型?MiniMind 全流程实战指南
人工智能·python·神经网络·ai·自然语言处理·大模型·模型训练微调
枫叶林FYL6 小时前
【Python高级工程与架构实战】项目四 现代ETL编排平台:Airflow + dbt + Snowflake 企业级数据管道架构与实现
人工智能·python·架构·etl
源码之屋6 小时前
计算机毕业设计:Python天气数据采集与可视化分析平台 Django框架 线性回归 数据分析 大数据 机器学习 大模型 气象数据(建议收藏)✅
人工智能·python·深度学习·算法·django·线性回归·课程设计
捧月华如6 小时前
React vs Vue vs Angular:三大前端框架深度对比
python·github
AI_Claude_code6 小时前
安全与合规核心:匿名化、日志策略与法律风险规避
网络·爬虫·python·tcp/ip·安全·http·网络爬虫