Django中自定义模板字符串

首先在目录中创建名为 templatetags 的文件夹

然后在文件中创建 python 文件

然后再相应的 HTML 模板文件中导入

复制代码
{% load python的文件名 %}

然后在 HTML 文件中通过进行使用

复制代码
{% 函数名 %}

python文件中的代码

首先导入
复制代码
from django import template

register = template.Library()
然后使用装饰器
复制代码
@register.filter
def 函数():
    pass

@register.simple_tag
def 函数(a1, a2):
    pass

@register.inclusion_tag('HTML文件目录')
def 函数():
    pass
相关推荐
曲幽1 天前
FastAPI 身份验证总踩坑?这份 FastAPI Users “避坑指南”请收好
python·fastapi·web·jwt·oauth2·user·authentication
装不满的克莱因瓶1 天前
掌握 RNN 与 LSTM 模型结构
人工智能·python·rnn·深度学习·神经网络·ai·lstm
jeffer_liu1 天前
Spring AI 生产级实战:裁判员
java·人工智能·后端·spring·大模型
何以解忧,唯有..1 天前
Python包管理工具pip:从入门到精通
开发语言·python·pip
金銀銅鐵1 天前
用 Tkinter 实现简单的猜数字游戏
后端·python
copyer_xyf1 天前
Python 模块与包的导入导出
前端·后端·python
ice8130331811 天前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
夜微凉41 天前
三、Spring
java·后端·spring
copyer_xyf1 天前
Python venv 虚拟环境
前端·后端·python
林爷万福1 天前
GitHub 开源光谱数据处理项目推荐
python·光纤光谱仪