Flask学习之二 模板
继续学习flask
本部分Miguel Grinberg教程的翻译地址:http://www.pythondoc.com/flask-mega-tutorial/templates.html
英文原文地址:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates
一、为什么使用模板?
以下做一个小小的扩展:输出一个大标题。
一个容易的选择就是改变我们的视图功能,输出 HTML。
修改app/views.py
from app import app @app.route('/')
@app.route('/index')
def index():
user = { 'nickname': 'Miguel'} # fake user
return '''
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>hello,''' + user['nickname'] + '''</h1>
</body>
</html>
'''
网页浏览器上的显示情况:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWYAAACTCAIAAAAybCRfAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu1dB1xTVxd/mUCAMMMeslSGICi4V1114d6rVq39rNZRF87WAe5ZZ13VquCoe6C4FfdAUZGNjDDDhuz33ZcEyMsOMkK9r/5oct+555z7v++dnHPuIoxwao0iqIBBz0BKCwoKvb09y8rKECVXJRkF/wC9voBA4xEJSsh0pZhAphIFXAFapQ+RSBAKq79poyXVOXh+cNGRvQ9y+NpU+0/QGhkZffjwycLCTP7xMKabPn4crbaVnTp1LC0pUkvWNAkIZD0KypV6yr6qGUQSERXU7hn9KrlaVCZX06LgQtS8TwZ8AvinBfvGJUX5XIG0BrW0F4AFN+3S+l2N25jGlA4eDIWPBzAEvq281Gr237UXoOkon8NVi4DGBEKBUGPaRiIkVssVCARUCrWR1IBidRoBCoUCHw+d7qEGVE5iMsBvSCWbTTOkYa4GvCACUgiAR8LQ0BA+HvChECOABSYg0iAiBPDX0sKCy+XyeDyIDkSgGgHwSIAHIyOjAj4e8KkACACTIfErXr9+CxGBCEAEIAKqEQCBiSidqfOjH6qbAe9CBCACDYMAUTxKIlQ3VtIw2kApEAGIgI4jALwLkZeh20PBOg4iVA8i8O0gIJmXIRp1hxdEACIAEVCDgCQwIWADJvCCCEAEIAJqEJAEJtBkqMEJ3oYIQARECNRMGG/cB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AB3UJAtJIVJD9hOkO3+gVqAxHQUQTAFjuiZAY0GTraQVAtiIBuIQAWv2MKoaiy3et1S12oDUQAItC4CAAvQzQjQwhHTBq3I6B0iEDTQIAsjkiIRJgHbRodBrWECDQuApJ5GUI4a7xx+wFKhwg0EQQke382EW2hmhABiEAjI1A9YRyuMWnknoDiIQJNAgGRyYCzxZtEX0ElIQI6gIAk6wlPfteBvoAqQASaAALYvAw4vtoEOgqqCBHQDQQwL0OUxoB2Qzc6BGoBEdBtBP4PUsJrUAMgimAAAAAASUVORK5CYII=" alt="" />
以下是教程原话:
我们暂时还不支持用户,所以暂时使用占位符的用户对象,有时也被称为假冒或模仿的对象。这样让我们可以集中关注应用程序的某一方面,而不用花心思在暂未完成的部分上。
我希望你同意我的说法,上面的解决方案是非常难看!如果我们需要返回一个含有大量动态内容的大型以及复杂的 HTML 页面的话,代码将会有多么复杂啊!如果你需要改变你的网站布局,在一个大的应用程序,该应用程序有几十个视图,每一个直接返回HTML?这显然不是一 个可扩展的选择。
二、初步使用模板
就是把前端和后端的分离。
文件 app/templates/index.html:
<html>
<head>
<title>{{title}} - microblog</title>
</head>
<body>
<h1>Hello, {{user.nickname}}!</h1>
</body>
</html>
注意一下index.html的位置
我们只是写了一个大部分标准的HTML页面,唯一的区别是有一些动态内容的在 {{ ... }} 中。
然后在视图函数(文件 app/views.py)中使用这些模板,修改 app/views.py 内容:
from app import app
from flask import render_template @app.route('/')
@app.route('/index')
def index():
user = { 'nickname': 'Miguel'} # fake user
return render_template("index.html",
title = 'Home',
user = user)
为了渲染模板,我们必须从 Flask 框架中导入一个名为 render_template 的新函数。此函数需要传入模板名以及一些模板变量列表,返回一个所有变量被替换的渲染的模板。
在内部,render_template 调用了 Jinja2 模板引擎,Jinja2 模板引擎是 Flask 框架的一部分。Jinja2 会把模板参数提供的相应的值替换了 {{...}} 块。
运行后的浏览器页面可以看到是和之前一样的。
关于模板中的控制和循环语句和其他语言没什么差别,很容易理解。
三、模板继承
我们知道多数网站都有导航栏,而同一个网站的导航栏都是一样的。在每个模板中复制粘贴同样的代码是很浪费时间,而且修改起来也很麻烦(修改一个部分就要把它的拷贝全部修改)。
教程原话:我们可以利用 Jinja2 的模板继承的特点,这允许我们把所有模板公共的部分移除出页面的布局,接着把它们放在一个基础模板中,所有使用它的模板可以导入该基础模板。
定义一个基础模板,该模板包含导航栏以及上面谈论的标题(文件 app/templates/base.html):
<html>
<head>
{% if title %}
<title>{{title}} - microblog</title>
{% else %}
<title>microblog</title>
{% endif %}
</head>
<body>
<div>Microblog: <a href="/index">Home</a></div>
<hr>
{% block content %}{% endblock %}
</body>
</html>
在这个模板中,我们使用 block 控制语句来定义派生模板可以插入的地方。块被赋予唯一的名字content。
然后修改我们的 index.html 模板继承自 base.html (文件 app/templates/index.html):
{% extends "base.html" %}
{% block content %}
<h1>Hi, {{user.nickname}}!</h1>
{% for post in posts %}
<div><p>{{post.author.nickname}} says: <b>{{post.body}}</b></p></div>
{% endfor %}
{% endblock %}
这样的话,之后如果要修改导航栏只需要修改一个地方就可以了。
Flask学习之二 模板的更多相关文章
- Python Flask学习笔记之模板
Python Flask学习笔记之模板 Jinja2模板引擎 默认情况下,Flask在程序文件夹中的templates子文件夹中寻找模板.Flask提供的render_template函数把Jinja ...
- Flask 学习篇二:学习Flask过程中的记录
Flask学习笔记: GitHub上面的Flask实践项目 https://github.com/SilentCC/FlaskWeb 1.Application and Request Context ...
- flask学习(九):模板渲染和参数传递
一. 如何渲染模板 1. 模板放在templates文件夹下 2. 从flask中导入render_template函数 3. 在视图函数中,使用render_template函数,渲染模板 注意:只 ...
- Flask 学习(二)jinja2模板介绍
控制语句和表达式 举例 Flask Python代码 from flask import Flask, render_template, redirect, request app = Flask(_ ...
- flask学习(二)
一.蓝图 作用:给开发者提供目录结构 功能:1.自定义模板.静态文件目录 2.给一类url加前缀 3.给一类url添加before_request 目录结构 from flask_werkzur ...
- Flask 学习(二)路由
Flask 路由 在说明什么是 Flask 路由之前,详细阐述下 Flask “Hello World” 这一 最小应用的代码. Flask “Hello World” from flask imp ...
- Flask 学习 十二 用户评论
评论在数据库中的表示 由于评论和2个模型有关系,分别是谁发了评论,以及评论了哪个文章,所以这次要更新数据库模型 models.py 创建用户评论数据库模型 class Comment(db.Model ...
- flask学习(十二):for循环遍历
一. 字典的遍历 语法和python一样,可以使用items().keys().values().iteritems().iterkeys().itervalues() {% for k, v in ...
- flask学习(十):模板中访问模型和字典的属性
访问模型中的属性或者是字典,可以通过{{params.property}}的形式,或者是使用{{params['age']}}这样的形式
随机推荐
- ztree树节点重叠问题
使用zTree时,由于同时使用了bootstrap插件,导致样式起了冲突,生成的树都挤在一起了, 最后的解决办法是设置zTreeStyle.css文件的.ztree li ul{}属性,在里面加入he ...
- 自定义确定框(confirm)
1.先引入 confirm.css @charset "UTF-8"; lq-alert { width: 100%; height: 100%; background: rgba ...
- Docker搭建的MySQL容器出现 "Too many connections 1040" 最大连接数修改完未生效的解决方案
原文:Docker搭建的MySQL容器出现 "Too many connections 1040" 最大连接数修改完未生效的解决方案 版权声明:本文为博主原创文章,未经博主允许不得 ...
- java-面向对象-封装-this-构造函数
概要图 一 构造函数 需求:为了描述事物更准确,发现事物对应的很多对象一创建时, 就有了,一些初始化的数据.在类中该如何完成的. 通过Java中的另一个小技术完成:就是构造函数.对象本身就是构造出 ...
- Laravel 中 offset,limit 或 skip , take 的使用
laravel 本身有一个自带的快速分页方法 paginate,只需要传入每页显示多少条数据就可以 了,但是如果想使用自定义从哪里开始呢. 一.offset,limit (offset 设置从哪里开始 ...
- Android 程序员不得不收藏的个人博客(持续更新...)
本文已收录我的 Github ,持续更新中 ,欢迎点赞 ! 每周打开一次收藏夹里的个人博客,已经成为了我的人生一大乐趣. 相比各大博客平台,我一直更加偏爱个人博客.在每个人自己的这一亩三分地里,你能看 ...
- 强力Django+杀手级xadmin开发在线教育网站
强力Django+杀手级xadmin开发在线教育网站采用 Python3.7全新开发 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的 ...
- 人物-旅行-潘德明-Info:《近代中国徒步环游世界第一人:潘德明 》
ylbtech-人物-旅行-潘德明-Info:<近代中国徒步环游世界第一人:潘德明 > 1.返回顶部 1. 近代中国徒步环游世界第一人:潘德明 2016-10-12 18:01 “以世界为 ...
- IDEA(JAVA)使用json
首先介绍一下json SON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度. JSON就是一串字符串 只不过元素会使用特定的 ...
- 10分钟学会Python
#1. 语法 Python中没有强制的语句终止字符,代码块是通过缩进来指示的.缩进表示一个代码块的开始,逆缩进则表示一个代码块的结束.一般用4个空格来表示缩进. 声明以冒号(:)字符结束,并且开启一个 ...