1. URL路由🍭🍭🍭
1.1 路由匹配标🍵🍵
1.11 使用url给视图函数传参数☕
path('index/', index)
path('detail/<int:id>/', index)
1.12 给url取别名☕
那么在使用此url的地方可以使用别名。
比如:
path('detail/<int:id>/', index, name='login')
# 在根路由中可以设置命名空间
path('app/', include(('App.urls', "App"), namespace='App'))
1.2 反向解析🍻🍻
1.21 在视图函数中,反向解析url🍯
from django.shortcuts import render, redirect, reverse
def buy(request):
return redirect(reverse('index'))
return redirect(reverse('detail', args=[2]))
return redirect(reverse('detail', kwargs={"id": 2}))
1.22 在templates中,使用别名🍯
{% url 'detail' stu.id %}
1.23 使用命名空间🍯
1. 在工程的urls.py文件中,在include时,可以指定命名空间,更加细化的划分url:
path('app/', include(('App.urls', "App"), namespace='App'))
2. 指定命令空间后,使用反向解析时需要加上命名空间,
在视图函数中:
return redirect(reverse('App:index'))
3. 在templates中:
{% url 'App:index' %}
2. 模板Template🍬🍬🍬
2.2 模板简介🍡🍡
在Django框架中,模板是可以帮助开发者快速生成呈现给用户页面的工具
模板的设计方式实现了我们MVT中VT的解耦,VT有着N:M的关系,一个V可以调用任意T,一个T可以供任意V使用
2.3 模板处理b吧🍰🍰
两个过程:
加载HTML、渲染数据
2.4 模板组成🍩🍩
两个部分:
1. HTML静态代码
静态页面:页面数据是本地固定的
动态页面:页面数据来源于后台服务器
2. 模板语言,动态插入的代码段(挖坑,填坑)
模板中的动态代码段除了做基本的静态填充,还可以实现一些基本的运算,转换和逻辑
2.5 模板变量🍪🍪
1. 视图传递给模板的数据,遵守标识符规则
语法: {{ var }}
如果变量不存在,则插入空字符串
2. 方法不能有参数
{{ str }}
{{ str.upper }}
{{ str.isdigit }}
{{ dict.key }}
3. 列表,使用索引,不允许负索引
items= ['apples', 'bananas', 'carrots']
{{ items.2 }}
2.6 模板标签🍥🍥
2.61 语法🥮
{% tag %}
2.62 作用🥮
加载外部传入的变量
在输出中创建文本
控制循环或逻辑
2.7 运用🥮
2.71 if 语句:
if单分支
{% if 表达式 %}
语句
{% endif %}
if双分支
{% if 表达式 %}
语句
{% else %}
语句
{% endif %}
if多分支
{% if 表达式 %}
语句
{% elif 表达式 %}
语句
{% else %}
语句
{% endif %}
2.72 判断true或false
{% if today_is_weekend %}
<p>Welcome to the weekend!</p>
{% endif %}
2.73 and or not
{% if athlete_list and coach_list %}
<p>Both athletes and coaches are available.</p>
{% endif %}
{% if not athlete_list %}
<p>There are no athletes.</p>
{% endif %}
{% if athlete_list or coach_list %}
<p>There are some athletes or some coaches.</p>
{% endif %}
2.74 in、not in
{% if "bc" in "abcdef" %}
This appears since "bc" is a substring of "abcdef"
{% endif %}
{% if user not in users %}
If users is a list, this will appear if user isn't an element of the list.
{% endif %}
2.75 for语句
# for 语句:
# 基础:
{% for 变量 in 列表 %}
语句1
{% empty %}
语句2
{% endfor %}
# 当列表为空或不存在时,执行empty之后的语句
# 进阶:
{{ forloop.counter }} # 表示当前是第几次循环,从1数数
{{ forloop.counter0}} # 表示当前是第几次循环,从0数数
{{ forloop.revcounter}} # 表示当前是第几次循环,倒着数数,到1停
{{ forloop.revcounter0}} # 表示当前第几次循环,倒着数,到0停
{{ forloop.first }} # 是否是第一个 布尔值
# 运用:
{% for object in objects %}
{% if forloop.first %}
<li class="first">
{% else %}
<li>
{% endif %}
{{ object }}</li>
{% endfor %}
{{ forloop.last }} # 是否是最后一个 布尔值
{% for link in links %}
{{ link }}{% if not forloop.last %} | {% endif %}
{% endfor %}
forloop.parentloop
{% for country in countries %}
<table>
{% for city in country.city_list %}
<tr>
<td>Country #{{ forloop.parentloop.counter }}</td>
<td>City #{{ forloop.counter }}</td>
<td>{{ city }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
2.72 判断true或false
{% if today_is_weekend %}
<p>Welcome to the weekend!</p>
{% endif %}
2.76 注释
注释:
单行注释
{# 被注释掉的内容 #}
多行注释
{% comment %}
内容
{% endcomment %}
2.77 过滤器
过滤器:
{{ var|过滤器 }}
作用:
在变量显示前修改
add {{ value|add:2 }}
# 没有减法过滤器,但是加法里可以加负数
{{ value|add:-2 }}
lower
{{ name|lower }}
upper
{{ my_list|first|upper }}
# 截断:
{{ bio|truncatechars:30 }}
过滤器可以传递参数,参数需要使用引号引起来
比如join:
{{ students|join:'=' }}
默认值:default,格式 {{var|default:value}}
如果变量没有被提供或者为False,空,会使用默认值
根据指定格式转换日期为字符串,处理时间的
就是针对date进行的转换
{{ dateVal | date:'y-m-d' }}
2.78 模板继承
模板继承
block:
{% block XXX%}
code
{% endblock %}
extends 继承,写在开头位置
{% extends '父模板路径' %}
include: 加载模板进行渲染
{% include '模板文件' %}
{{ block.super }} : 获取父模板中block中的内容
2.79 其它
HTML转义:
将接收到的数据当成普通字符串处理还是当成HTML代码来渲染的一个问题
渲染成html:
{{ code|safe }}
关闭自动转义
{% autoescape off%}
code
{% endautoescape %}
打开自动转义转义
{% autoescape on%}
code
{% endautoescape %}