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 %}
相关推荐
nbsaas-boot2 小时前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
仗剑_走天涯2 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
chao_7892 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
Nejosi_念旧2 小时前
解读 Go 中的 constraints包
后端·golang·go
风无雨2 小时前
GO 启动 简单服务
开发语言·后端·golang
小明的小名叫小明2 小时前
Go从入门到精通(19)-协程(goroutine)与通道(channel)
后端·golang
斯普信专业组2 小时前
Go语言包管理完全指南:从基础到最佳实践
开发语言·后端·golang
一只叫煤球的猫4 小时前
【🤣离谱整活】我写了一篇程序员掉进 Java 异世界的短篇小说
java·后端·程序员
你的人类朋友5 小时前
🫏光速入门cURL
前端·后端·程序员
chao_7897 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找