接:https://blog.csdn.net/u010132177/article/details/103788677 参考:https://docs.djangoproject.com/zh-hans/3.0/contents/ https://docs.djangoproject.com/zh-hans/3.0/intro/overview/#write-your-viewshtml
<!--花括号内是变量名,对应views.py中的字典键名--> <h1>{{ hello }}----{{wa}}</h1>
从模板中咱们知道变量使用了双括号。python
[BASE_DIR+"/templates",]
或 [os.path.join(BASE_DIR,'templates')],
,以下所示:/project1/settings.py 文件代码:django
...TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR+"/templates",], # 修改位置 或[os.path.join(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', ], }, }, ] ...
注:BASE_DIR来自前面定义,即当前项目的绝对路径: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
app
from django.shortcuts import render #渲染模块 #from django.http import HttpResponse # Create your views here. def index(request): # request为必备参数 context={} #【1】定义1个字典 context['hello']='hello world!!!' #【2】向字典写一个键:值(hello:'hello world!!') context['wa']='wawawawawahahahaha!' return render(request,'app1/index.html',context) #【3】返回:把context渲染到app1/index.html的模板文件 #return HttpResponse('hello python!')
hello world!----wawawawawahahahaha!测试
【2.2】定义一个字典值为一个列表,list为把内容转换为列表spa
from django.shortcuts import render def index(request): context={} #【1】定义1个字典 context['hello']='hello world!!!' #【2】向字典写一个键:值(hello:'hello world!!') context['wa']='wawawawawahahahaha!' context['list']=list(range(1,10)) #【2.2】定义一个字典值为一个列表,list为把内容转换为列表 return render(request,'app1/index.html',context) #【3】返回:把context渲染到app1/index.html的模板文件
重点:【循环出列表】.net
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>app1模板首页</title> </head> <body> <!--括号内是变量名,对应views.py中的字典键名--> <h1 style="color:red">{{ hello }}----{{wa}}</h1> <br/> 【循环出列表】:{{list}} <ul> {% for i in list %} <li>{{i}}</li> {% endfor %} </ul> </body> </html>
{% if condition %} ... display {% endif %} # 或者: {% if condition1 %} ... display 1 {% elif condition2 %} ... display 2 {% else %} ... display 3 {% endif %}
{% if %} 标签接受 and , or 或者 not 关键字来对多个变量作判断 ,或者对变量取反( not ),例如:debug
{% if athlete_list and coach_list %} athletes 和 coaches 变量都是可用的。 {% endif %}
与Python的 for 语句的情形相似,循环语法是 for X in Y ,Y是要迭代的序列而X是在每个特定的循环中使用的变量名称。 每一次循环中,模板系统会渲染在 {% for %} 和 {% endfor %} 之间的全部内容。 例如,给定一个运动员列表 athlete_list 变量,咱们可使用下面的代码来显示这个列表:code
<ul> {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} </ul>
{% for athlete in athlete_list reversed %} ... {% endfor %}
{% for athlete in athlete_list %} <h1>{{ athlete.name }}</h1> <ul> {% for sport in athlete.sports_played %} <li>{{ sport }}</li> {% endfor %} </ul> {% endfor %}
{% ifequal %} 标签比较两个值,当他们相等时,显示在 {% ifequal %} 和 {% endifequal %} 之中全部的值。htm
下面的例子比较两个模板变量 user 和 currentuser :
{% ifequal user currentuser %} <h1>Welcome!</h1> {% endifequal %}
和 {% if %} 相似, {% ifequal %} 支持可选的 {% else%} 标签:8
{% ifequal section 'sitenews' %} <h1>Site News</h1> {% else %} <h1>No News Here</h1> {% endifequal %}
Django 注释使用 {# #}。
{# 这是一个注释 #}
模板过滤器能够在变量被显示前修改它,过滤器使用管道字符,以下所示:
{{ name|lower }}
{{ name }} 变量被过滤器 lower 处理后,文档大写转换文本为小写。
{{ my_list|first|upper }}
以上实例将第一个元素并将其转化为大写。
例如:
{{ bio|truncatewords:"30" }}
这个将显示变量 bio 的前30个词。
{{ pub_date|date:"F j, Y" }}
{% include %} 标签容许在模板中包含其它的模板的内容。 下面这个例子都包含了 nav.html 模板:
{% include "nav.html" %}
接下来咱们先建立以前项目的 templates 目录中添加 base.html 文件,代码以下: HelloWorld/templates/base.html 文件代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p>菜鸟教程 Django 测试。</p> {% block mainbody %} <p>original</p> {% endblock %} </body> </html>
以上代码中,名为 mainbody 的 block 标签是能够被继承者们替换掉的部分。 全部的 {% block %} 标签告诉模板引擎,子模板能够重载这些部分。
HelloWorld/templates/hello.html 文件代码:
{%extends "base.html" %} {% block mainbody %} <p>继承了 base.html 文件</p> <p>{{hello}}</p> {% endblock %}
第一行代码说明 hello.html 继承了 base.html 文件。能够看到,这里相同名字的 block 标签用以替换 base.html 的相应 block。
菜鸟教程 Django 测试。 继承了 base.html 文件 Hello World!