Flask中render_template的使用和模板的继承
2017-06-16 11:03
369 查看
这就是传说中的MVC:Model-View-Controller,中文名“模型-视图-控制器”。
Python处理URL的函数就是C:Controller,Controller负责业务逻辑,比如检查用户名是否存在,取出用户信息等等;
包含变量{{ name }}的模板就是V:View,View负责显示逻辑,通过简单地替换一些变量,View最终输出的就是用户看到的HTML。
MVC中的Model在哪?Model是用来传给View的,这样View在替换变量的时候,就可以从Model中取出相应的数据。
模板:
模板的位置放在templates文件夹下面,一般是html文件,我们把index.html改动成如下样式
<html> <head> <title>{{title}} - microblog</title> </head> <body> <h1>Hello, {{user.nickname}}!</h1> </body> </html>1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
其中: {{}}表示这是一个变量,可以根据用户在模块端给予的参数的不同,进行调整
下面的程序就是调用了 render_template模板
from flask importrender_template from app import app @app.route('/') @app.route('/index') def index(): user = {'nickname': 'Miguel'} # fake user return render_template("index.html", title = 'Home', user = user) #这里模块里的第一个user指的是html里面的变量user,而第二个user指的是函数index里面的变量user1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
说白了,其实render_template的功能是先引入index.html,同时根据后面传入的参数,对html进行修改渲染。
然后,render_template模板其实也是接受控制语句的,修改后的index.html如下:
<html> <head> {% if title %} #{% %}这样代表控制语句,意思是如果有传入title变量,则显示title-microblog <title>{{title}} - microblog</title> {% else %} <title>Welcome to microblog</title> #如果没有传入参数,则显示welcome to microblog {% endif %} #这里大不同!!HTML里面的逻辑语句,需要用{% endif %} 来结束逻辑语句 </head> <body> <h1>Hello, {{user.nickname}}!</h1> </body> </html>1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
我们再来看一个循环语句的修改版的views.py和index.html
def index(): user = {'nickname': 'Miguel'} # fake user posts = [# fake array of posts { 'author': { 'nickname': 'John' }, 'body': 'Beautiful day in Portland!' }, { 'author': { 'nickname': 'Susan' }, 'body': 'The Avengers movie was so cool!' } ] return render_template("index.html", title = 'Home', user = user, posts = posts)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
我们再来看一个循环语句的修改版的views.py和index.html
def index(): user = {'nickname': 'Miguel'} # fake user posts = [# fake array of posts { 'author': { 'nickname': 'John' }, 'body': 'Beautiful day in Portland!' }, { 'author': { 'nickname': 'Susan' }, 'body': 'The Avengers movie was so cool!' } ] return render_template("index.html", title = 'Home', user = user, posts = posts)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
注意,这里的访问都是用点来继承的,比如post.author.nickname
最后,如果说在以后的开发中,模板变得非常多,但是每个模板中,都有固定项目是不能变的,那如何操作呢….
比如一个页面顶端有按钮,在其他页面中也有,那不可能每个页面中都写吧
于是,我们就把一样的部分,都放在了base.html这个基本模板中,通过{% block content %}这个接口,来和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>1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{% extends "base.html" %} #通过这句话,来进行继承挂钩,和主体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 %}1
2
3
4
5
6
7
1
2
3
4
5
6
7
相关文章推荐
- Flask中render_template的使用和模板的继承
- Flask中render_template的使用和模板的继承
- javascript模板引擎template.render使用
- Flask 学着用模板 render_template
- VTemplate模板引擎的使用--入门篇
- c++ 模板 迭代器 使用问题解决方法(too few template-parameter-lists)
- artTemplate--模板使用自定义函数(1)
- 解决 "Use a cube template" is dimmed (disabled) "使用立方模板" 选项不可选(变灰)(被禁用)的问题
- Django之模板继承与ajax使用错误处理~
- 动态模板类使用 ITemplate 接口 GridView动态模板
- Grid使用模板(template)出错 (备忘)
- template模板使用规则 C++
- Springer LNCS LaTeX投稿模板使用方法 How to use the Springer LNCS LaTeX template
- vim模板插件vim-template的使用
- adminTemplate后台模板插件使用文档
- C++中 模板Template的使用
- flask中的session,render_template()第二和参数是字典
- Silverlight使用DataGrid的模板列(DataGridTemplateColumn)实现类似TreeListView控件的效果
- Android——一分钟学会使用实时模板Live Template
- C++中 模板Template的使用