第三章 Django模板

一、创建Template

from django import template
t = template.Template('my name is {{name}}')
c = template.Context({'name':'Nige'})

 二、模板标签和过滤器

1、if/else : 

{% if today_is weekend and(or) bb%}

<p>hello</p>

{% elif%}

<p>hello3</p>

{% else %}

<p>hello1</p>

{% endif %}

2、for:

{% for athlete in athlete_list%}

{{athlete.name}}

{% empty %}

<p>for循环为空判断,跳出循环</p>
{% endfor %}

 

{{ forloop.counter }}:循环次数,从1开始

{{ forloop.counter0 }}:循环次数,从0开始

{{ forloop.revcounter }}:倒序循环次数,最后一次为1

{{ forloop.revcounter }}:倒序循环次数,最后一次为0

{{ forloop.first }}:布尔值,判断是否是循环第一次,例子:

{% for object in objects %}

   {% if forloop.first %}

        <li class='first'>hello</li>

   {% endif %}

   <li>hello</li>

   {% if forloop.last %}

       <li class='last'>hello</li>

  {% endif %}

{% endfor %}

{{ forloop.last }}:布尔值,判断是否是循环的最后一次,例子如上

{{ forloop.parentloop }}:在嵌套循环中获取上级forloop对象,例如:

{% for i in loop1%}

   {% for j in loop2 %}

       <p>loop1:{{forloop.parentloop.counter}}</p>

       <p>loop2:{{forloop.counter}}</p>

   {% endfor %}

{% endfor %}

3、{% ifequal %}:比较两个值,如果相等显示{%ifequal%} {%endifequal%}之间的值,例如

{% ifequal user1 user2 %}

    <p>equal</p>

{%else%}

    <p>not equal</p>

{%endifequal%}

4、注释

{#  注释内容  #}

多行注释:

{% comment %}

  注释内容

{% endcomment %}

比较user1与user2是否相等

5、过滤器

{{ name|lower }}:通过lower过滤器过滤name值,将name值转为小写

{{ name|first|upper }}:取name的第一个元素,并将其转为大写

{{ name|truncatewords:"30" }}:取变量name的前30个字母

 

三、在视图中使用模板

1、settings.py文件中template设置

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
说明如下:
BACKEND:值是一个点分Python路径,指向实现Django模板后端API引擎类,有django.template.backends.django.DjangoTemplates和django.templates.backends.jinja2.Jinja2
DIRS:定义一个目录列表,模板引擎按顺序查找模板文件
APP_DIRS:设定是否在安装的APP中查找模板,为True时,模板引擎在INSTALLED_APP中个应用中查找名为“templates”子目录
OPTIONS:针对后端的一些设置
2、设置模板路径
在settings.py中的TEMPLATES设置
'DIRS': [os.path.join(BASE_DIR,'templates')]

3、视图使用render
views.py:
from django.shortcuts import render
import datetime
def current_time(request):
    now = datetime.datetime.now()
    return render(request,'current_datetime.html',locals())

  4、current_datetime.html

{{now}}

5、include模板标签

{% include%}:作用是引入另一个模板,如:

{% include 'nav.html' %}

6、模板继承

定义base.html模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
<h1>My helpful timestamp site</h1>
{% block content %}{% endblock %}
{% block footer %}
<hr>
<p>Thanks for visiting my site.</p>
{% endblock %}
</body>
</html>
base.html

current_datetime.html

{% extends 'base.html' %}

{% block title %} The current time {% endblock %}

{% block content %}
  <p>It is now {{ now }}</p>
{% endblock %}
current_datetime.html

说明:

如果有{% extend %}必须放在模板的第一个标签

使用{% block.super %}从父模板中获取内容

posted @ 2019-11-16 23:23  blf  阅读(116)  评论(0编辑  收藏  举报