django 模板两次for循环_Django模板这么用才好!

本文详细介绍了Django模板的使用,包括如何通过settings.py配置模板路径,使用render替代HttpResponse实现数据与视图分离。讲解了模板中的变量、列表、字典以及if/else和for标签的用法,提供了实例代码展示如何在模板中操作这些数据结构,并展示了实际运行结果。
摘要由CSDN通过智能技术生成

ae81e551a56ac0727563523c4b91f867.png

django.http.HttpResponse()来输出" hello smart_wyn !"。该方式将数据与视图混合在一起,不符合Django的MVC思想。

本章节我们将为大家详细介绍Django模板的应用,模板是一个文本,用于分离文档的表现形式和内容。

我们接着上一章节的项目将在WeChat_public目录底下创建templates目录并建立wyn.html文件,整个目录结构如下:

75455e6c611131ca7b8da8e722f4a006.png

wyn.html:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>wyn templates</title>
</head>
<body>
<h1>{{ wyn_arg }}</h1>
    <h2>{{ wyn_arg }}</h2>
        <h3>{{ wyn_arg }}</h3>
</body>
</html>

从HTML模板中我们知道变量使用双括号展示。接下来我们需要向Django说明模板文件的路径,修改WeChat_public/settings.py,修改TEMPLATES中的DIRS为[BASE_DIR+"/templates",],如下所示:

settings.py:

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [BASE_DIR + "/templates",],  # 添加模板文件位置
     'APP_DIRS': True,
    'OPTIONS': {
      'context_processors': [
          'django.template.context_processors.debug',
          'django.template.context_processors.request',
          'django.contrib.auth.context_processors.auth',
         'django.contrib.messages.context_processors.messages',
],
     },
  },
]

我们现在修改views.py,增加一个新的对象,用于向模板提交数据:

from django.shortcuts import render

def v_1(request):
   message = 'hello smart_wyn ! this is a templates !'
return render(request, "wyn.html",{'wyn_arg':message})

可以看到,我们这里使用render来替代之前使用的HttpResponse。运行程序,访问http://127.0.0.1:8000/hello,可以看到页面:

956cdb995abd569e70ce0aa5dd36f14f.png

这样我们就完成了使用模板来输出数据,从而实现数据与视图分离。接下来我们将具体介绍模板中常用的语法规则。变量:

views.py:{"HTML变量名": "views变量名"}HTML:{{变量名}}上面示例便是使用变量语法列表:

templates中的wyn.html中,可以用.索引下标取出对应的元素。

views.py:

from django.shortcuts import render

def v_1(request):
   message = ['hello smart_wyn !', 'this is a templates !']
   return render(request, "wyn.html",{'arg_list':message})

wyn.html:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>wyn templates</title>
</head>
<body>
<h1>{{ arg_list }}</h1> {#取出整个列表 #}
    <h2>{{ arg_list.0 }}</h2> {#取出列表第0个元素 #}
        <h3>{{ arg_list.1 }}</h3> {#取出列表第1个元素 #}
/body>
</html>

运行结果如下:

88f1d07ee2a2c5f1975ce2cad5a2ebf6.png

字典:

templates中的wyn.html中,可以用.键取出对应的值。

views.py

from django.shortcuts import render

def v_1(request):
   message = {'name':'smart_wyn','age':18}
return render(request, "wyn.html",{'arg_dict':message})

wyn.html:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>wyn templates</title>
</head>
<body>
<h1>{{ arg_dict }}</h1> {#取出整个字典 #}
    <h2>{{ arg_dict.name }}</h2> {#取出字典name的值 #}
        <h3>{{ arg_dict.age }}</h3> {#取出字典age的值 #}
/body>
</html>
</html>

运行结果如下:

fc52d9533d6dfcd548dfce9b665507d3.png

if/else标签:

基本语法格式如下:

{% if condition %}

... display

{% endif%}

或者:

{% if condition1 %}

... display 1

{% elif condition2 %}

... display 2

{% else%}

... display 3

{% endif%}

根据条件判断是否输出。if/else支持嵌套。{% if %}标签接受and,or或者not关键字来对多个变量做判断,或者对变量取反(not )。此处不做示例,可自行验证。

for 标签

基本语法格式如下:

{% for %}

... display
{% endfor %}

{% for %}允许我们在一个序列上迭代。与Python的for语句的情形类似,循环语法是for i in a,a是要迭代的序列而i是在每一个特定的循环中使用的变量名称。

每一次循环中,模板系统会渲染在{% for %}和{% endfor %}之间的所有内容。可以直接用字典.items方法,用变量的解包分别获取键和值。

views.py:

from django.shortcuts import render

def v_1(request):
   message = [{'name':'han mei mei','age':18},
            {'name':'li lin','age':23},
            {'name':'xiao hua','age':31}]
   return render(request, "wyn.html",{'arg_dl':message})

wyn.html:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>wyn templates</title>
</head>
<body>
% for i in arg_dl %}
    {% if i.age <= 18%}
<h1>{{ i.name }} juvenile</h1>
    {% elif i.age > 18 and i.age < 30%}
<h1>{{ i.name }} youth</h1>
    {% else %}
<h1>{{ i.name }} middle aged</h1>
    {% endif %}
{% endfor %}

{% for i,j in arg_dl.0.items %}
<h2>{{ i }} : {{ j }}</h2>
% endfor %}
/body>
</html>
</html>

运行结果如下:

271cd0e951ef9f6ab6bee8e0f09bd0e5.png

<---------------- END -------------->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值