使用Django中的标签可以在模板中做一些简单的逻辑判断。
格式:{%标签%}
for标签
for 标签和python中的for循环,执行原理相同
格式:
{%for 临时变量 in 变量%}
....
{%empty%}
....
{% end for%}
程序如下:
html.py
文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li>{{ name }}</li>
{% empty %}
<li>空空如也</li>
{% endfor %}
</ul>
</body>
</html>
views.py
文件:
def laowang6(request):
# name_list = ['安其拉', '鲁班','狄仁杰'] #不是空列表
name_list=[]
return render(request, 'laowang6.html', {'name_lst': name_list})
urls.py
文件:
from django.contrib import admin
from django.urls import path
from demo01 import views
urlpatterns = [
path('laowang6/', views.laowang6),
for循环可用的一些参数:
Variable | Description |
---|---|
forloop.counter | 当前循环的索引值(从1开始) |
forloop.counter0 | 当前循环的索引值(从0开始) |
forloop.revcounter | 当前循环的倒序索引值(从1开始) |
forloop.revcounter0 | 当前循环的倒序索引值(从0开始) |
forloop.first | 当前循环是不是第一次循环(布尔值) |
forloop.last | 当前循环是不是最后一次循环(布尔值) |
执行流程:
如果变量不存在或者为空,则执行empty中的内容。否则执行for循环体中的内容。
HTML
文件
<ul>
{% for game in lst %}
<li>
{{ forloop.counter }}<--->{{ game }}, {# 索引从1开始#}
{{ forloop.counter0 }}<--->{{ game }} ,{# 索引从0开始#}
{{ forloop.revcounter }}<--->{{ game }}, {# 索引倒序#}
{{ forloop.revcounter0 }}<--->{{ game }}, {# 索引倒序#}
{{ forloop.first }}, {# 当前循环是否是第一次#}
{{ forloop.last }}, {# 当前循环是否是最后一次#}
</li>
{% endfor %}
</ul>
views.py
文件
def for_tag(request):
lst = ['王者荣耀', '穿越火线', '英雄联盟', '吃鸡']
lst1 = ['aa', ['bb', 'cc']]
return render(request, '02-标签.html', {'lst': lst, 'lst1': lst1})
url(r'^for_tag/', views.for_tag), # for_tag 标签
3.if 标签
if标签 和python中的if判断执行流程相同
格式:
{% if 判断条件 %}
...
{% endif %}
注: if 语句支持 and、or、==、<、>、!=、<=,>=,in、not in、is、is not判断。
html.py
文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>dddd</title>
</head>
<body>
{% if name|length > 5 %}
用户名符合标准
{% endif %}
</body>
</html>
views.py
文件:
def laowang7(request):
return render(request,'laowang7.html',{'name':'HELLOyu'})
urls.py
文件:
from django.contrib import admin
from django.urls import path
from demo01 import views
urlpatterns = [
path('laowang7/', views.laowang7),]
(4)if … else 标签
格式:
{% if name_length|length > 6 %}
...
{% else %}
...
{% endif %}
html.py
文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if name|length > 4 %}
用户名长度符合规则
{% else %}
用户名成都不符合长度规则
{% endif %}
</body>
</html>
views.py
文件:
from django.shortcuts import HttpResponse,render
def laowang1(request):
return render(request,'laowang1.html',{'name':'laowang1'})
urls.py
文件:
from django.contrib import admin
from django.urls import path
from demo01 import views
urlpatterns = [
path('laowang1/', views.laowang1),
]
(5).if…elif…else 标签
格式:
{% if name_length|length > 6 %}
…
{% elif age == 20 %}
…
{% else %}
…
{% endif %}
html.py 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if name|length > 20 %}
你的名字太长了
{% elif age == 38 %}
你还年轻呢
{% else %}
老腊肉
{% endif %}
</body>
</html>
urls.py 文件
from django.contrib import admin
from django.urls import path
from demo01 import views
urlpatterns = [
path('laowang2/', views.laowang2),
views.py 文件
from django.shortcuts import render
def laowang2(request):
return render(request,'laowang2.html',{'name':'老王','age':38})
(6). load
和 static
标签
- load标签用来加载static标签
- static用来加载静态文件(图片、css、js)
- 一般情况下将静态文件存放到static目录下(需要手动创建static目录)
- 在 settings.py 中让django加载到搜索路径
当我们想在页面上使用一些静态资源例如图片、js、css 。这时候我们就要使用到static标签了。
例如在页面中显示一张本地图片。
操作步骤:
a.新建一个文件夹来盛放静态资源,例如 static,为了和后面的static标签区分,这里使用 statics名称
b.将statics文件夹加载到python搜索路径中。要设置setting.py
文件,在STATIC_URL = '/static/'
下面添加如下:
STATIC_URL = '/static/'
#将自定义statics添加路径
STATICFILES_DIRS=[os.path.join(BASE_DIR, 'statics')]
c.在页面上最开始位置使用 {%load static %} load 标签 来加载 static 标签。
<!DOCTYPE html>
{#加载static标签#}
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<img src="{% static '278437.jpg' %}" alt="熊宝爱生活" width="500" height="500">
</body>
</html>
views.py 文件
from django.shortcuts import render
def laowang3(request):
return render(request,'laowang3.html')
urls.py 文件
from django.contrib import admin
from django.urls import path
from demo01 import views
urlpatterns = [
path('laowang2/', views.laowang2),
d.在要展示图片的地方 使用 {% static ‘图片路径’}
{% static ‘图片路径’} 表示 使用
注:如果不想在每一个页面中都使用 {%load static %} ,可在settings.py中进行如下配置