Flask provides a view template that separates business logic from performance logic, conforms to people's development habits, and facilitates maintenance.
Here you'll learn how to render the template, and then pass the dynamic Data to the template.
FLASK provides the Render_template function, which is first introduced: From flask import flask, render_template, the first parameter in this function is the template path file name, and all subsequent parameters are key value pairs passed to the template.
The data portion of the page template is passed by using key-value pairs when routing returns a template, as we define one:
@app. Route ('/')
def index (): Return
render_template ('/index.html ', name= ' Zhangsan ')
The page template uses {{}} to receive, {}} to indicate that the package is a data variable, the above method corresponds to receive the way:
<a>{{name}}</a>
We usually pass a set of data, such as we pass an array:
@app. Route ('/')
def index ():
num = [' Tom ', ' Mike ', ' Amuxia ', ' Zhao ', ' Lisi '] return
render_template ('/ Index.html ', Num=num)
How to walk him out of the template:
<ul>
{% for name in num%}
<li>{{name}}</li>
{% endfor%}
</ul>
Sometimes traversing the data we want to do some processing in the display, this time we can use the JINJA2 provided by the filter to escape
As in the example above, we want the name to be traversed in uppercase:
<ul>
{% for name in num%}
<li>{{name|upper}}</li>
{% endfor%}
</ul>
A few commonly used filters
Capitalize: The first letter of the string university, the rest lowercase. Trim: Go to space. Lower: turn to lowercase. Upper: Capitalize.