Table of Contents
1.5 {% with %}:用更简单的变量名替代复杂的变量名
{% load staticfiles %} is deprecated in favor of {% load static %}.
一、tag标签的使用
在views.py中,定义视图:
def show(request):
s = stu('Tom', 23) # name age
list=['Tom','Jok',"ja'r're"]
time = datetime.datetime.now()
sex = False #女
a = "http://www.baidu.com"
return render(request, "time.html" , locals())
1.1 {% if %} 的使用
在time.html文件中,使用标签:
{% if s.age > 20 %}
<h1>{{ s.name }}年龄大于20</h1>
{% else %}
<h1>{{ s.name }}年龄小于20</h1>
{% endif %}
{% if %} 标签接受and,or或者not来测试多个变量值或者否定一个给定的变量
{% if %} 标签不允许同一标签里同时出现and和or,否则逻辑容易产生歧义,例如下面的标签是不合法的:
{% if obj1 and obj2 or obj3 %}
1.2 {% for %}的使用
{% for i in list %}
<li>{{ i }}</li>
{% endfor %}
可反序:
{% for i in list reversed %}
<li>{{ i }}</li>
{% endfor %}
1.3 forloop模板变量
{% for %}标签内置变量,含有一些属性可以提供给你一些关于循环的信息。forloop变量只能在循环中得到,当模板解析器到达{% endfor %}时forloop就消失。
-
forloop.counter 表示循环的次数,它从1开始计数
{% for i in list reversed %}
<li>{{ forloop.counter }}: {{ i }}</li>
{% endfor %}
####################
1: ja'r're
2: Jok
3: Tom
-
forloop.counter0 类似于forloop.counter,但它是从0开始计数
{% for i in list reversed %}
<li>{{ forloop.counter }}: {{ i }}</li>
{% endfor %}
###########################
0: ja'r're
1: Jok
2: Tom
-
forloop.revcounter 反向循环计数, 最小的值仍然是1
{% for i in list reversed %}
<li>{{ forloop.revcounter }}: {{ i }}</li>
{% endfor %}
###########################
3: ja'r're
2: Jok
1: Tom
-
forloop.revcounter0 反向循环计数, 最小的值是0
{% for i in list reversed %}
<li>{{ forloop.revcounter }}: {{ i }}</li>
{% endfor %}
###########################
2: ja'r're
1: Jok
0: Tom
-
forloop.first True/False 当第一次循环时值为True
{% for i in list reversed %}
{% if forloop.first %}
<li> 第一个元素是:{{ i }}</li>
{% endif %}
{% endfor %}
################
第一个元素是:ja'r're
-
empty 判断当前元素是否是空
#在views.py中,我们将list列表设置为None
def show(request):
list=None
...
#在time.py中,我们添加{% empty %}
{% for i in list reversed %}
{% if forloop.first %}
<li> 第一个元素是:{{ i }}</li>
{% endif %}
{% empty %}
<h1>当前元素是None</h1>
{% endfor %}
################
当前元素是None
需要注意的是,判断是列表元素list是否是None,而不是内容(如:list[0])是否是None
1.4 {% url %}: 引用路由配置的地址
前面都使用过,这个方法,这里我们再做一下:
先看转发条目:【主路由urls.py】
path('blog/', include('blog.urls'), name="bl")
转向的是我们自定义的blog.urls.py文件中的路由:
path("home/", views.blog, name='bl'),
然后我们在time.html中,添加:
<a href="{% url 'bl' %}">转向我的博客首页</a>
由于views.py中定义的show最后是render转向time.html的,所以这里我们访问show路径:
==》
1.5 {% with %}:用更简单的变量名替代复杂的变量名
{% with total=fhjsaldfhjsdfhlasdfhljsdal %} {{ total }} {% endwith %}
1.6 {% verbatim %}: 禁止浏览器渲染变量
在show函数中,传入参数:
a = "<span><a href='#'>http://www.baidu.com</a></span>"
然后我们在time.html中测试:
{% verbatim %}
{{ a }}
<li>列表</li>
{% endverbatim %}
不难看出,不渲染的是我们定义的变量,而对于HTML标签,还是能够正常显示的。
1.7 {% load %}: 加载标签库
{% load staticfiles %} is deprecated in favor of {% load static %}.
{% load staticfiles %}是我们之前使用过的这里个内容上,使用ctrl+鼠标,追踪。
发现,其实本质就是一个标签库:
import warnings
from django import template
from django.templatetags.static import (
do_static as _do_static, static as _static,
)
from django.utils.deprecation import RemovedInDjango30Warning
register = template.Library()
def static(path):
warnings.warn(
'django.contrib.staticfiles.templatetags.static() is deprecated in '
'favor of django.templatetags.static.static().',
RemovedInDjango30Warning,
stacklevel=2,
)
return _static(path)
@register.tag('static')
def do_static(parser, token):
warnings.warn(
'{% load staticfiles %} is deprecated in favor of {% load static %}.',
RemovedInDjango30Warning,
)
return _do_static(parser, token)
上面有一句警告:{% load staticfiles %} is deprecated in favor of {% load static %}.
告诉我们{% load staticfiles %}已经弃用了,建议使用 {% load static %}。所以我们以后使用 {% load static %}。
1.8 模仿上面标签类,我们这里也可以自定义标签
参考:https://blog.csdn.net/qq_40326481/article/details/80342480
参考:http://www.cnblogs.com/yuanchenqi/articles/6083427.html
参考:https://blog.csdn.net/qq_42737056/article/details/84249066
1.8.1 在我的app下建立templatetags Python包:
上面我还建立了我的自定义tag文件my_tags.py文件:
from django import template
from django.utils.safestring import mark_safe
register = template.Library() #register的名字是固定的,不可改变
#过滤器
@register.filter(name='multi')
def filter_multi(v1,v2):
return v1 * v2
#标签
@register.simple_tag(name='weizu')
def show_nick_name():
return mark_safe("WEIZU")
然后需要在settings.py文件中声明:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig', #pycharm自动生成的app路径
'blog.templatetags' #这里是我们需要配置的tag路径
]
然后在路由表中添加路由条目,然后转向HTML模板,在HTML中应用,这里还是选取我们的time.html文件:
{% load my_tags %} #载入自定义标签库
<h1>{{ num | multi:2 }}</h1> <!--过滤器-->
<h1>nickname1 : {% weizu %}</h1> <!--标签 simple tag-->
作者:无涯明月