Django 自定义过滤器

1,编写自定义过滤器并注册

创建目录

Test/app5/templatetags

分别创建文件

Test/app5/templatetags/init.py

Test/app5/templatetags/myfilter.py

添加过滤器脚本

Test/app5/templatetags/myfilter.py

复制代码
from django import template
register = template.Library()
@register.filter
def show_title(value, n):
    if len(value) > n:
        return f'{value[:n]}...'
    else:
        return value

2,添加视图函数

Test/app5/views.py

复制代码
from django.shortcuts import render

# Create your views here.
import datetime

def filter(request):
    str1 = 'abcdefg'
    str2 = 'ABCDEFGHIJKLMNO'
    str3 = '123456789'

    time_str = datetime.datetime.now()
    return render(request, '5/filter.html', {'str1':str1, 'str2':str2, 'str3':str3, 'time_str':time_str})


def html_filter(request):
    html_addr = """
        <table border='2'>
            <tr>
                <td>
                这是表格A
                </td>
                <td>
                这是表格B
                </td>
            </tr>
        </table>
    """

    html_script = """
        <script language='JavaScript'>
            document.write('非法执行');
        </script>
    """

    return render(request, '5/html_filter.html', {"html_addr":html_addr, "html_script":html_script})


def diy_filter(request):
    dict1 = {'标题': '学习Django天天加班,熬到深夜,沉迷学习无法自拔!'}
    dict2 = {'标题': '学习java天天加班,熬到深夜,沉迷学习无法自拔!'}
    dict3 = {'标题': '学习C++天天加班,熬到深夜,沉迷学习无法自拔!'}
    lists = [dict1, dict2, dict3]
    return render(request, '5/diy_filter.html', {"lists":lists})

3,添加路由

Test/app5/urls.py

复制代码
from django.urls import path
from . import views


urlpatterns = [
    path('filter', views.filter, name='filter'),
    path('html_filter', views.html_filter, name='html_filter'),
    path('diy_filter', views.diy_filter, name='diy_filter'),
]

4,添加HTML代码

Test/templates/5/diy_filter.html

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% load myfilter %} <!-- 使用半角字符 -->
<table border="1" style="width: 300px">
    {% for list in lists %}
    <tr>
        <td>
            {{ list.标题|show_title:10 }} <!-- 假设 show_title 过滤器接收一个参数 -->
        </td>
    </tr>
    {% endfor %}
</table>

</body>
</html>

5,访问页面

http://127.0.0.1:8000/app5/diy_filter

相关推荐
毕胜客源码9 小时前
卷积神经网络的农作物识别系统(有技术文档)深度学习 图像识别 卷积神经网络 Django python 人工智能
人工智能·python·深度学习·cnn·django
ch_atu10 小时前
序列化器的使用
django
计算机徐师兄16 小时前
Python基于Django的创新实验室系统(附源码,文档说明)
python·django·创新实验室系统·python创新实验室系统·创新实验室·实验室系统·python实验室系统
源码之家17 小时前
计算机毕业设计:Python股票智能分析预测平台 Flask框架 数据分析 可视化 机器学习 随机森林 大数据(建议收藏)✅
python·机器学习·数据分析·django·flask·课程设计
B站_计算机毕业设计之家17 小时前
计算机毕业设计:Python股票投资辅助决策系统 django框架 request爬虫 协同过滤算法 数据分析 可视化 大数据 大模型(建议收藏)✅
爬虫·python·深度学习·算法·django·flask·课程设计
天天进步20151 天前
Python全栈项目:从零构建基于 Django 的知识管理系统(KMS)
开发语言·python·django
刀法如飞1 天前
一款Python语言Django框架DDD脚手架,开箱即用
python·架构·django
源码之家2 天前
计算机毕业设计:Python股票交易管理可视化系统 Django框架 requests爬虫 数据分析 可视化 大数据 大模型(建议收藏)✅
爬虫·python·深度学习·信息可视化·数据分析·django·课程设计
一 乐2 天前
智慧社区|基于Python + Django智慧社区系统(源码+数据库+文档)
java·数据库·python·django·论文·毕设·智慧社区系统
源码之屋2 天前
计算机毕业设计:Python天天基金数据采集与智能分析平台 Django框架 数据分析 可视化 爬虫 大数据 大模型(建议收藏)✅
人工智能·爬虫·python·数据分析·django·flask·课程设计