This is the legendary Mvc:model-view-controller, Chinese name "model-View-controller".
Python's function of handling URLs is C:controller,controller is responsible for business logic, such as checking the existence of user names, removing user information and so on;
The template that contains the variable {{name}} is the v:view,view responsible for displaying the logic, and by simply replacing some variables, the view ultimately outputs the HTML that the user sees.
Where is the model in MVC? The model is used to pass to the view, so that when the view replaces the variable, it can take the corresponding data out of the model.
Template:
The location of the template is placed under the Templates folder, usually the HTML file, we change the index.html to the following style
Where: {{}} indicates that this is a variable that can be adjusted according to the parameters given by the user at the module-side
The following program calls the Render_template template
From the 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, and the second user refers to the variable user in the function index
Frankly speaking, in fact, the function of Render_template is to introduce index.html, and then modify the rendering of HTML according to the parameters passed in later.
Then, the render_template template is actually accept the control statement, the modified index.html as follows:
Let's look at a modified version of views.py and index.html for a loop statement.
def index ():
user = {' nickname ': ' Miguel '} # Fake user
posts = [# Fake array of posts
{
' author ': {' nickn ' Ame ': ' John '},
' body ': ' Beautiful day in portland! '
},
{'
author ': {' nickname ': ' Susan '},
' body ': ' The Avengers movie is so cool! '
}
]
Return Render_template ("index.html",
title = ' Home ',
user = user,
posts = posts)
Let's look at a modified version of views.py and index.html for a loop statement.
def index ():
user = {' nickname ': ' Miguel '} # Fake user
posts = [# Fake array of posts
{
' author ': {' nickn ' Ame ': ' John '},
' body ': ' Beautiful day in portland! '
},
{'
author ': {' nickname ': ' Susan '},
' body ': ' The Avengers movie is so cool! '
}
]
Return Render_template ("index.html",
title = ' Home ',
user = user,
posts = posts)
Note that the visits here are inherited by points, such as Post.author.nickname
Finally, if in the future development, the template becomes very much, but each template, there are fixed items can not change, how to operate it ....
For example, there is a button on the top of the page, there are other pages, it is not possible to write on each page
So, we put the same part in the base.html this basic template, through the {% block content%} this interface, and base.html to do the link
{% extends "base.html"%} #通过这句话, to carry out inheritance hooks, and principal base.html to link
{% block content%}