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,不能是其它

        

from django import template

register = template.Library()

【5】使用装饰器

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)文件中的内容

    <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即可)

{% load comon_left %}

{% left user.username %}

相关推荐

  1. Django抽取(inclusion_tag)

    2023-12-14 05:36:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-14 05:36:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-14 05:36:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-14 05:36:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-14 05:36:01       18 阅读

热门阅读

  1. Kubernetes集群Pod介绍

    2023-12-14 05:36:01       33 阅读
  2. MySQL_10.MySQL体系架构

    2023-12-14 05:36:01       34 阅读
  3. Windows系统使用wsl执行shell脚本报错解决

    2023-12-14 05:36:01       51 阅读
  4. 在ajax中如何使用jquery循环。

    2023-12-14 05:36:01       41 阅读
  5. Spring Security(一)架构概览

    2023-12-14 05:36:01       31 阅读
  6. ARM TZC-400原理及配置方式

    2023-12-14 05:36:01       35 阅读
  7. ARM按键中断

    2023-12-14 05:36:01       30 阅读
  8. 理解和应用 Golang 中的 TCP 网络编程

    2023-12-14 05:36:01       31 阅读