建立DJANGO的自定义TAG

简介: DJANGO的TAG分为三类: • simple_tag : Processes the data and returns a string• inclusion_tag : Processes the data and returns a rendered template• assignmen...

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 template

register = template.Library()

from ..models import Post
from django.db.models import Count


@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.assignment_tag
def get_most_commented_posts(count=5):
    return Post.published.annotate(total_comments=Count('comments')).order_by('-total_comments')[:count]

latest_posts.html

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

base.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 %}
        <h3>Most commented post</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>

无css层展示

目录
相关文章
|
8月前
|
前端开发 API 数据格式
Django的restframework接口框架自定义返回数据格式
在前后端分离是大趋势的背景下,前端获取数据都是通过调用后台的接口来获取数据微服务的应用越来越多。Django是Python进行web应用开发常用的web框架,用Django框架进行web应用框架减少了很多工作,通常用很少量的代码就可以实现数据的增、删、改、查的业务应用,同样用Django的restframework的框架对外发布接口也是非常的简单方便,几行代码就可以将数据对象通过接口的方式提供服务。因为在实际开发过程中接口的返回数据有一定的格式,本文介绍通过自定义Response返回对象来自定义接口返回数据格式。
97 1
|
25天前
|
数据处理 数据库 开发者
Django中的自定义管理命令:扩展管理功能的途径
【4月更文挑战第15天】Django教程:介绍如何创建和使用自定义管理命令以扩展框架功能。在应用的`management/commands`目录下创建Python文件,继承`BaseCommand`,实现`handle`方法。示例代码展示了如何定义参数和执行逻辑。自定义命令适用于批量数据处理、定期任务、项目初始化和自定义迁移操作。注意文件位置、命令安全性和稳定性。自定义管理命令能提升开发和维护效率。
|
10月前
|
网络架构 Python
【Django学习】(十四)自定义action_router
【Django学习】(十四)自定义action_router
|
10月前
|
Python
【Django学习】(九)自定义校验器_单字段_多字段校验_模型序列化器类
【Django学习】(九)自定义校验器_单字段_多字段校验_模型序列化器类
|
12月前
|
数据库 Python
django drf 实现只有超级用户才能注册账号(涉及自定义权限permissions,获取token信息解析token信息)
django drf 实现只有超级用户才能注册账号(涉及自定义权限permissions,获取token信息解析token信息)
|
12月前
|
Python
Django框架开发004期 Python编程调用自定义Django框架template模板网页
Django框架开发004期 Python编程调用自定义Django框架template模板网页
|
前端开发 关系型数据库 MySQL
Django 自定义装饰器解决MySQL server has gone away错误
Django 自定义装饰器解决MySQL server has gone away错误
80 0
|
测试技术 Python
Django Template层之自定义tag
Django Template层之自定义tag
48 0
|
测试技术 API Python
Django 通过自定义context_processors实现自定义tag
Django 通过自定义context_processors实现自定义tag
44 0
|
Python
Django自定义错误页面
1.修改系统配置 需要修改settings.py文件中的两个配置: 将DEBUG设置为False,标识当前处于非调试模式 非调试模式下需要指定ALLOWED_HOSTS,这里将它简单地设置为[‘*’],代表允许所有的域名访问
88 0
Django自定义错误页面