【Youtobe trydjango】Django2.2教程和React实战系列七【模板templates和Django模板引擎】
【Youtobe trydjango】Django2.2教程和React实战系列七【模板templates和Django模板引擎】
- 1. 修改视图函数
- 2. 新建模板文件夹和html文件
- 3. 项目配置文件模板路径添加
- 4. 模板触类旁通
- 5. 模板引擎
- 5.1. 模板引擎探究
- 5.2. 增加基础页
- 5.3. 引入模板标签
- 5.4. 模板上下文context渲染
- 5.5. 模板标签语言的循环结构
- 5.6. 模板标签语言的条件判断
- 5.7. 模板标签语言的过滤器
1. 修改视图函数
pages/views.py
home_view
函数的返回值改成render渲染html后的对象:
def home_view(request, *args, **kwargs): # *args, **kwargsprint(args, kwargs)print(request.user)# return HttpResponse("<h1>Hello World!</h1>") # string of HTML codereturn render(request, "home.html", {})
2. 新建模板文件夹和html文件
templates
文件夹与项目主文件夹trydjango
和其他应用products
和pages
同级,并在里面新建主页模板文件home.html
templates\home.html
<h1>Hello World!</h1>
<p>This is a template!</p>
3. 项目配置文件模板路径添加
settings.py
我们先随便写一个字符串进去看一下错误的报什么错:
报错如下,从而得知拼接路径的规律:
因此应该拼接产生一个绝对的静态文件路径进去
'DIRS': [os.path.join(BASE_DIR, 'templates')],
效果
4. 模板触类旁通
将pages\views.py里面视图函数对应的模板全部复写一遍:
from django.http import HttpResponse
from django.shortcuts import render# Create your views here.def home_view(request, *args, **kwargs): # *args, **kwargsprint(args, kwargs)print(request.user)# return HttpResponse("<h1>Hello World!</h1>") # string of HTML codereturn render(request, "home.html", {})def contact_view(request, *args, **kwargs):print(args, kwargs)return render(request, "contact.html", {})def about_view(request, *args, **kwargs):return render(request, "about.html", {})def social_view(request, *args, **kwargs):return render(request, "social.html", {})
然后每个页面对应的模板都创出来:
保存重启开发服务器测试:
5. 模板引擎
5.1. 模板引擎探究
先来看下Django的模板引擎长什么样子:
templates\home.html
<h1>Hello World!</h1>
{{ request.user.is_authenticated }}
<p>This is a template!</p>
修改静态文件无需重启即可查看前端效果:
无痕页面:
5.2. 增加基础页
templates\base.html
<!doctype html>
<html>
<head><title>Coding for learning Django and React</title>
</head>
<body>{% block content %}replace me{% endblock %}
</body>
</html>
templates\home.html
主页命名为content
的block
块中的内容将替换base.html
页面的content
块:
{% extends 'base.html' %}{% block content %}
<h1>Hello World!</h1>
{{ request.user.is_authenticated }}
<p>This is a template!</p>
{% endblock %}
效果
如果about.html
不复写这个content块(将content块注释掉),则有如下效果:
{% extends 'base.html' %}{% comment %} {% block content %} {% endcomment %}
<h1>Hello World!</h1>
<p>This is a About Page!</p>
{% comment %} {% endblock %} {% endcomment %}
同样,其他页面都一样引入base.html,并复写content块
5.3. 引入模板标签
首先创建一个templates\navbar.html
<nav><ul><li>Brand</li><li>Contact</li><li>About</li></ul>
</nav>
然后在templates\base.html
中引入
{% include 'navbar.html' %}
效果
这样如果需要对导航栏navbar
作修改,只需要修改templates\navbar.html
这一个地方
5.4. 模板上下文context渲染
templates\about.html
{% extends 'base.html' %}{% block content %}
<h1>Hello World!</h1>
<p>This is a About Page!</p><p>{{ my_text }}, my age is {{ my_age }}, {{ my_list }}
</p>{% endblock %}
pages\views.py
def about_view(request, *args, **kwargs):my_context = {'my_text': "This is about me",'my_age': 23,'my_list': [177, 61, 23]}return render(request, "about.html", my_context)
效果
5.5. 模板标签语言的循环结构
templates\about.html
添加如下代码:
<ul>
{% for my_sub_item in my_list %}<li>{{ forloop.counter }} - {{ my_sub_item }}</li>
{% endfor %}
</ul>
传过来的字典(json)是context,包含键my_list
,因此,在前端可以直接取用,并用for循环将元素取到,还可以在循环体内部用{{ forloop.counter }}
取到循环次数
效果
5.6. 模板标签语言的条件判断
templates\about.html
<ul>
{% if my_list %}
{% for my_sub_item in my_list reversed %}{% if my_sub_item == 61 %}<li>{{ forloop.counter }}. 我的体重是{{ my_sub_item | add:my_sub_item }}KG</li>{% elif my_sub_item == 177 %}<li>{{ forloop.counter }}. 我的身高是{{ my_sub_item }}CM</li>{% else %}<li>{{ forloop.counter }}. 我的腰围是{{ my_sub_item }}CM</li>{% endif %}
{% endfor %}
{% else %}暂无数据!
{% endif %}
</ul>
这里展示了一层和多层判断结构,和模板标签过滤器add方法
先将pages\views.py
的视图函数about_view传给前端的上下文my_context的键my_list注释掉,重启开发服务器:
正确的结果,
5.7. 模板标签语言的过滤器
可以带着关键词去搜索更全的过滤器,这里展示一下:
capfirst
首字母大写
{{ my_text | capfirst }}
upper
全大写
{{ my_text | upper}}
此处不作多展示
【Youtobe trydjango】Django2.2教程和React实战系列七【模板templates和Django模板引擎】相关推荐
- 【Youtobe trydjango】Django2.2教程和React实战系列四【创建Django应用】
[Youtobe trydjango]Django2.2教程和React实战系列四[创建Django应用] 1. 创建应用 2. 修改应用 1. 创建应用 打开cmd黑框,也可以用下列方法打开项目根目 ...
- 【Youtobe trydjango】Django2.2教程和React实战系列二【settings配置文件】
[Youtobe trydjango]Django2.2教程和React实战系列二[settings配置文件] 1. Django项目初始化过程 2. 全貌 3. 详细解释 4. 增加其他配置 1. ...
- 【Youtobe trydjango】Django2.2教程和React实战系列五【python shell操作models模型】
[Youtobe trydjango]Django2.2教程和React实战系列五[python shell操作models模型] 1. 用python shell查询新增models对应的数据 2. ...
- 【Youtobe trydjango】Django2.2教程和React实战系列十【动态路由、app内部路由】
[Youtobe trydjango]Django2.2教程和React实战系列十[动态路由.app内部路由] 1. 动态路由示例 1.1 动态路由 1.2 处理DoesNotExist不存在 2. ...
- 【Youtobe trydjango】Django2.2教程和React实战系列六【自定义主页、路由与请求】
[Youtobe trydjango]Django2.2教程和React实战系列六[自定义主页.路由与请求] 1. 初始化主页应用 2. 修改视图 3. 项目url增加主页路由 4. 路由 1. 初始 ...
- 【Youtobe trydjango】Django2.2教程和React实战系列一【项目简介 | 搭建 | 工具】
[Youtobe trydjango]Django2.2教程和React实战系列一[项目简介 | 搭建 | 工具] 1.环境与选型说明 2.技术栈选型说明 3.django搭建详解 3.1. 项目虚拟 ...
- 【Youtobe trydjango】Django2.2教程和React实战系列三【Django超级管理员和内置内容】
[Youtobe trydjango]Django2.2教程和React实战系列三[Django超级管理员和内置内容] 1. Django数据初始化及超级管理员 2. 用户模块使用 1. Django ...
- 【Youtobe trydjango】Django2.2教程和React实战系列八【渲染数据库数据与模板加载顺序探究】
[Youtobe trydjango]Django2.2教程和React实战系列八[渲染数据库数据与模板加载顺序探究] 1. 准备数据 2. 渲染数据库数据到模板 3. 如何在app里加载django ...
- React Native 系列(七) -- ListView
前言 本系列是基于React Native版本号0.44.3写的.几乎所有的App都使用了ListView这种组件,这篇文章将学习RN中ListView的平铺样式和分组样式. ListView平铺样式 ...
最新文章
- 学Java需要学哪些书?
- SAP Cloud Platform上Destination属性为odata_gen的具体用途
- 百度网页移动端html,百度移动端开始用网站品牌名代替网址显示
- TCP socket心跳包示例程序
- UVA 1411 - Ants(二分图完美匹配)
- hdu 2089 数位dp入门
- Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(
- C#多线程之线程同步篇2
- Go语言系列——Go语言介绍
- CSS——响应式网页(Bootstrap)
- 仓库实现降本增效的秘密法宝,WMS智能仓储系统
- Redux Reducer
- [论文笔记]Fader Networks: Manipulating Images by Sliding Attributes(2017 NIPS)
- 移动云mas 通过HTTP请求发送普通短信和 模板短信
- 二分查找理论(三种问题类型、两种算法形式)
- 没有技术含量,但能每天赚50刀的GGAD赚钱办法分享
- Xcode 12 Beta 3 更新记录SwiftUI2
- ios友盟错误_iOS - 友盟错误分析
- 计算机cpu和显卡的搭配要求,CPU和显卡怎么搭配好 显卡和CPU搭配有什么要求吗...
- C++ 11 后一些便捷用法
热门文章
- [VirtualBox][Wireshark]如何在虚拟机ubuntu系统中使用Wireshark
- python找与7相关的数_查找连续有七个“ 7”的所有10位质数-Python
- KDZD土壤电阻率测试仪
- 机器人理论基础 Chapter 1 - Introduction
- 哈工大计算机系统Lab2.二进制炸弹
- TP6 腾讯云发送短信验证码配置详解
- Windows桌面右键反应卡顿解决
- 5G时代的“轻终端、重云端”
- CVPR 2020学术竞赛大盘点,中国团队揽获众多冠军
- 华为3G e220 linux