python Tags 母板 组件 静态文件相关 自定义simpletag inclusion_tag_weixin_33842304的博客-程序员资料

技术标签: python  

一.Tags
(一)for
1.基本用法
<ul>
{% for user in user_list %}
<li>{ { user.name }}</li>
{% endfor %}
</ul>
2.for循环可用的一些参数
forloop.counter 当前循环的索引值(从1开始)
forloop.counter0 当前循环的索引值(从0开始)
forloop.revcounter 当前循环的倒序索引值(从1开始)
forloop.revcounter0 当前循环的倒序索引值(从0开始)
forloop.first 当前循环是不是第一次循环(布尔值)
forloop.last 当前循环是不是最后一次循环(布尔值)
forloop.parentloop 本层循环的外层循环
(二)for...empty
<ul>
{% for user in user_list %}
<li>{ { user.name }}</li>
{% empty %}
<li>空空如也</li>
{% endfor %}
</ul>

(三)if elif 和else
{% if user_list %}
用户人数:{ { user_list|length }}
{% elif black_list %}
黑名单数:{ { black_list|length }}
{% else %}
没有用户
{% endif %}
(四)if ...else
{% if user_list|length > 5 %}
七座豪华SUV
{% else %}
黄包车
{% endif %}
if语句支持 and 、or、==、>、<、!=、<=、>=、in、not in、is、is not判断。
(五)with
定义一个中间变量
{% with p_list.0.name as chenrun %}
{ {chenrun}}
(六)csrf_token
这个标签用于跨站请求伪造保护。
在页面的form表单里面写上{% csrf_token %}
(七)注意事项
1. Django的模板语言不支持连续判断,即不支持以下写法:
{% if a > b > c %}
...
{% endif %}
可以写成 a>b and b>c
2. Django的模板语言中属性的优先级大于方法
def xx(request):
d = {"a": 1, "b": 2, "c": 3, "items": "100"}
return render(request, "xx.html", {"data": d})
如上,我们在使用render方法渲染一个页面的时候,传的字典d有一个key是items并且还有默认的 d.items() 方法,
此时在模板语言中: { { data.items }} 默认会取d的items key的值。
二.母板
我们通常会在母板中定义页面专用的CSS块和JS块,方便子页面替换
(一).继承母板
在子页面中在页面最上方使用下面的语法来继承母板。
语法: {% extends 'layouts.html' %}
(二)块(block)
通过在母板中使用{% block xxx %}来定义"块"。
{% block page_panel %}
<h3 class="panel-title">出版社列表</h3>
{% endblock %}
在子页面中通过定义母板中的block名来对应替换母板中相应的内容
{% block page_panel %}
<h3 class="panel-title">书名列表</h3>
{% endblock %}
三.组件
可以将常用的页面内容如导航条,页尾信息等组件保存在单独的文件中,然后在需要使用的地方按如下语法导入即可。
{% include 'navbar.html' %}
四.静态文件相关
(一)第一种方式static
1.导入
{% load static %}
2.使用
<script src="{% static "mytest.js" %}"></script>
注意:某个文件多处被用到可以存为一个变量
{% load static %}
{% static "images/hi.jpg" as myphoto %}
<img src="{ { myphoto }}"></img>
(二)第二种方式 get_static_prefix
{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!" />
或者:
{% load static %}
{% get_static_prefix as STATIC_PREFIX %}
<img src="{ { STATIC_PREFIX }}images/hi.jpg" alt="Hi!" />
五.自定义simpletag
类似于自定义filter,只不过接收更灵活的参数
1.在app下创建templatetags
2.在templatetags下创建mydefination.py
3.注册 simple tag
@register.simple_tag(name="plus")
def plus(a, b, c):
return "{} + {} + {}".format(a, b, c)
4.使用自定义simple tag
{% load mydefination %}
{% plus "1" "2" "abc" %}
六.inclusion_tag
多用于返回html代码片段
1.在app下创建templatetags
2.在templatetags下创建mydefination.py
3.templatetags注册 inclusion_tag
@register.inclusion_tag('result.html')
def show_results(n):
n = 1 if n < 1 else int(n)
data = ["第{}项".format(i) for i in range(1, n+1)]
return {"data": data}
4.templates/result.html
<ul>
{% for choice in data %}
<li>{ { choice }}</li>
{% endfor %}
</ul>
5.templates/index.html
<body>
{% load my_inclusion %}

{% show_results 10 %}
</body>

转载于:https://www.cnblogs.com/J-7-H-2-F-7/p/9630643.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_33842304/article/details/93262242

智能推荐

OCP-1Z0-051 第130题 LEFT OUTER JOIN的用法_hello_holly的博客-程序员资料

一、原题View the Exhibit and examine the data in the EMPLOYEES table:You want to display all the employee names and their corresponding manager names.Evaluate the following query:SQL> SELECT e.emp

Windows Server 2012 在桌面上显示”我的电脑_我是高手高手高高手的博客-程序员资料

如果是在Windows Server 2012本地控制台下,直接按Win(键盘上的微软徽标键)+R,输入:rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0回车后,勾选所需要的桌面图标的名称,确定即可。

​程序员离职后收到原公司 2400 元,被告违反竞业协议赔 18 万_唐普开发者程序员:cxyITgc的博客-程序员资料

​程序员离职后收到原公司 2400 元,被告违反竞业协议赔 18 万!到底是怎么回事?

IOS开发之----异常处理_沸腾的泪水05314的博客-程序员资料

转载自:http://blog.sina.com.cn/s/blog_71715bf8010166qf.html开篇大话: Object-C语言的异常处理符号和C++、JAVA相似。再加上使用NSException,NSError或者自定义的类,你可以在你的应用程序里添加强大的错误处理机制。异常处理机制是由这个四个关键字支持的:@try,@catch,@thorw,@finally。当代码

node相关报错问题_gyp info using [email protected]_lily1346891的博客-程序员资料

问题一:node-sass npm ERR! command failed解决:1、删除 npm uninstall node-sass2、安装 npm install node-sass问题二:npm ERR! gyp info it worked if it ends with oknpm ERR! gyp info using [email protected] ERR! gyp info using [email protected] | win32 | x64npm ERR! gyp ERR!

Android 连接USB默认选中MTP模式_Just_Paranoid的博客-程序员资料

Android 连接USB默认选中MTP模式需求分析Android系统默认连接USB会显示:正在通过USB为此设备充电,并且无法在电脑查看存储内容。需要实现的效果:Android 连接USB默认选中MTP模式,连接USB显示:正在通过USB传输文件,选择USB的使用方式的弹框下MTP模式为选中状态,并且可以在电脑端可以访问和写入存储空间。解决方案diff --git a/frameworks/base/services/usb/java/com/android/server/usb/UsbD

随便推点

shell脚本练习题_weixin_30421809的博客-程序员资料

下面列出了几个linux学习中的shell脚本题目1、九九乘法表#!/bin/bashecho "九九乘法表"#注意((之间不能有空格、加减乘除的格式、还有转义字符\*、-nefor (( i=1; i&lt;=9; i=i+1 ))do for (( j=1; j&lt;=i; j=j+1 )) do ((result=$i*$j)) ec...

matlab分段函数中大于、小于(“」”、“「”)的应用_matlab小于_viafcccy的博客-程序员资料

代码范例clc,clear;T=0.001;k1=-1:T:5;f1=2*((k1&gt;0)-(k1&gt;4)); %f1(t)的 MATLAB描述k2=-1:T:3;f2=(k2&gt;0)-(k2&gt;2); %f2(t)信号的描述%画图程序subplot(3,1,1)plot(k1,f1);axis([-1,5,0,2.2]) %f1的显示范围title('f1');

mysql安装图解 mysql图文安装教程(详细说明)_浮生(FS)的博客-程序员资料

很多朋友刚开始接触mysql数据库服务器,下面是网友整理的一篇mysql的安装教程,步骤明细也有详细的说明。MySQL5.0版本的安装图解教程是给新手学习的,当前mysql5.0.96是最新的稳定版本。mysql 下载地址 http://www.jb51.net/softs/2193.html下面的是MySQL安装的图解,用的可执行文件安装的

java面试(进阶四篇)解答_恐龙弟旺仔的博客-程序员资料

题目来自于网络,答案是笔者整理的。仅供参考,欢迎指正来源: https://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&amp;amp;mid=2247485779&amp;amp;idx=1&amp;amp;sn=3b06b9923df7f40f887ead8b8a53e50e&amp;amp;chksm=e9c5f0e2deb279f47fbfc3a12a70896bf95fa8c...

mac中安装git并忽略.DS_Store_mac .gitignore 忽略ds__q2826621520的博客-程序员资料

一Homebrew安装git1.安装 Homebrew/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"2.安装gitbrew install git二xcode安装git1.在mac终端中直接输入git.提示是否安装开发者...