Django之侧边栏抽取(inclusion_tag)

include和inclusion_tag的区别:

1、include:固定的,不能动态变化


2、inclusion_tag:返回一个动态的html片段 ----- 编写方式和自定义过滤器差不多

【1】编写步骤1

【1】在settings中得INSTALLED_APPS配置当前app,不然django无法找到自定义的标签


【2】在app中创建templatetags包(包名只能叫templatetags)


【3】在包下创建任意的py文件


【4】导入,实例化得到对象,对象名字必须是register,不能是其它

python 复制代码
from django import template

register = template.Library()

【5】使用装饰器

python 复制代码
from django import template
from BLOG import models
from django.db.models import Count
from django.db.models.functions import TruncMonth
# 侧边栏抽取
register = template.Library()

# 把返回的数据----渲染在left.html中-------html片段
# 把这个html片段放在你想放在的位置
@register.inclusion_tag('left.html', name='left')
def left(username):
    user = models.UserInfo.objects.filter(username=username).first()
    res_category = models.Article.objects.filter(blog_id=user.blog.id).values('category__id').annotate(
        article_count=Count('id')).values('category__id', 'category__name', 'article_count')
    res_tag = models.Article.objects.filter(blog_id=user.blog.id).values('tag__id').annotate(
        article_count=Count('id')).values('tag__id', 'tag__name', 'article_count')
    res_date = models.Article.objects.filter(blog_id=user.blog.id).annotate(create_date=TruncMonth('create_time')).values(
        'create_date').annotate(article_count=Count('id')).values('create_date', 'article_count')
    return {'user':user,'res_category':res_category,'res_tag':res_tag,'res_date':res_date}

【6】html片段,(left.html)文件中的内容

python 复制代码
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">我的标签</h3>
        </div>
        <div class="panel-body">
            {% for tag in res_tag %}
                <p>
                    <a href="/{{ user.username }}/tag/{{ tag.tag__id }}.html">{{ tag.tag__name }}({{ tag.article_count }})</a>
                </p>
            {% endfor %}

        </div>
    </div>
    <div class="panel panel-success">
        <div class="panel-heading">
            <h3 class="panel-title">随笔分类</h3>
        </div>
        <div class="panel-body">
            {% for category in res_category %}
                <p>
                    <a href="/{{ user.username }}/category/{{ category.category__id }}.html">{{ category.category__name }}({{ category.article_count }})</a>
                </p>
                <hr>
            {% endfor %}


        </div>
    </div>
    <div class="panel panel-info">
        <div class="panel-heading">
            <h3 class="panel-title">随笔档案</h3>
        </div>
        <div class="panel-body">
            {% for date in res_date %}
                <p><a href="/{{ user.username }}/archive/{{ date.create_date|date:'Y-m' }}.html">{{ date.create_date|date:'Y-m' }}({{ date.article_count }})</a></p>
            {% endfor %}

        </div>
    </div>

【7】在模板中使用(在想用的位置load即可)

html 复制代码
{% load comon_left %}

{% left user.username %}
相关推荐
亿牛云爬虫专家27 分钟前
Kubernetes下的分布式采集系统设计与实战:趋势监测失效引发的架构进化
分布式·python·架构·kubernetes·爬虫代理·监测·采集
ai小鬼头3 小时前
Ollama+OpenWeb最新版0.42+0.3.35一键安装教程,轻松搞定AI模型部署
后端·架构·github
萧曵 丶4 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
老任与码4 小时前
Spring AI Alibaba(1)——基本使用
java·人工智能·后端·springaialibaba
蹦蹦跳跳真可爱5894 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
nananaij5 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
雷羿 LexChien5 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
华子w9089258595 小时前
基于 SpringBoot+VueJS 的农产品研究报告管理系统设计与实现
vue.js·spring boot·后端
星辰离彬5 小时前
Java 与 MySQL 性能优化:Java应用中MySQL慢SQL诊断与优化实战
java·后端·sql·mysql·性能优化
敲键盘的小夜猫5 小时前
LLM复杂记忆存储-多会话隔离案例实战
人工智能·python·langchain