您的位置:首页 > 房产 > 建筑 > Django 自定义标签

Django 自定义标签

2024/12/23 8:52:34 来源:https://blog.csdn.net/qq_26086231/article/details/140068547  浏览:    关键词:Django 自定义标签

1,简单标签

1.1 添加自定义标签函数

Test/app5/templatetags/mytags.py

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

 

1.2 添加视图函数

from django.shortcuts import render# Create your views here.
import datetimedef 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})def diy_tags(request):dict1 = {'标题': '学习Django天天加班,熬到深夜,沉迷学习无法自拔!'}dict2 = {'标题': '学习java天天加班,熬到深夜,沉迷学习无法自拔!'}dict3 = {'标题': '学习C++天天加班,熬到深夜,沉迷学习无法自拔!'}lists = [dict1, dict2, dict3]return render(request, '5/diy_tags.html', {"lists":lists})

1.3 添加路由地址

Test/app5/urls.py

from django.urls import path
from . import viewsurlpatterns = [path('filter', views.filter, name='filter'),path('html_filter', views.html_filter, name='html_filter'),path('diy_filter', views.diy_filter, name='diy_filter'),path('diy_tags', views.diy_tags, name='diy_tags'),
]

1.4 添加html代码

Test/templates/5/diy_tags.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>{% load mytags %} <!-- 使用半角字符 -->
<table border="1" style="width: 300px">{% for list in lists %}<tr><td>{% show_title list.标题 10 %}</td></tr>{% endfor %}
</table></body>
</html>

1.5 访问页面

http://127.0.0.1:8000/app5/diy_tags

2,包含标签

2.1 添加自定义标签函数

Test/app5/templatetags/mytags.py

from django import template
register = template.Library()
@register.simple_tag()
def show_title(value, n):if len(value) > n:return f'{value[:n]}...'else:return value@register.inclusion_tag("5/show_info_tags2.html")
def show_info_tags2():dict1={'标题':'小强|2024-06-29'}dict2={'标题':'小强|2024-06-29'}dict3={'标题':'小强|2024-06-29'}lists=[dict1,dict2,dict3]return {'lists':lists}

2.2 添加html代码

Test/templates/5/show_info_tags2.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><table border="1" style="width: 310px">{% for list in lists %}<tr><td>{{ list.标题 }}</td></tr>{% endfor %}
</table></body>
</html>

2.3 添加视图函数

Test/app5/views.py

from django.shortcuts import render# Create your views here.
import datetimedef 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})def diy_tags(request):dict1 = {'标题': '学习Django天天加班,熬到深夜,沉迷学习无法自拔!'}dict2 = {'标题': '学习java天天加班,熬到深夜,沉迷学习无法自拔!'}dict3 = {'标题': '学习C++天天加班,熬到深夜,沉迷学习无法自拔!'}lists = [dict1, dict2, dict3]return render(request, '5/diy_tags.html', {"lists":lists})def show_info_2(request):return render(request, '5/show_info_2.html')

2.4 添加html代码

Test/templates/5/show_info_2.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>{% load mytags %}
<p>当前文件是show_info_2</p>
<p>以下内容是从另外一个模版文件show_info_tags2加载的</p>
{% show_info_tags2 %}
</body>
</html>

2.5 添加路由地址

Test/app5/urls.py

from django.urls import path
from . import viewsurlpatterns = [path('filter', views.filter, name='filter'),path('html_filter', views.html_filter, name='html_filter'),path('diy_filter', views.diy_filter, name='diy_filter'),path('diy_tags', views.diy_tags, name='diy_tags'),path('show_info_2', views.show_info_2, name='show_info_2'),]

2.6 访问页面

http://127.0.0.1:8000/app5/show_info_2

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com