A flask-based web application is born in the second article. This article mainly introduces how to use the jinja2 template engine and wtf form plug-in, which has a certain reference value, if you are interested, you can refer to the content in chapter 1 and make some simple pages. First, use this method to create a login page, first, create a login routing method:
@app.route("/login",methods=["GET"])def login(): html="
The code is very simple, and the style is not beautifying. In fact, for me, it is only because of smart perception that there is enough reason to use the template. then, for default. the Python login method is modified as follows:
From flask import render_template # Header, introduce the template rendering method @ app. route ("/login", methods = ["GET"]) def login (): return render_template ("/login.html") find the login.html file under the templatesfolder
Because the code in the html template is the same as the code directly written in the py file, the page is refreshed and the display effect is the same as that in the previous step. Although the display effect is not significantly improved, however, it is much easier to modify an element in html.
The jinja2 template engine also supports some more powerful functions, such as using index to provide some instructions:
Basic usage
Modify some codes in default. py:
From flask import render_template # page header, import the rendering function @ app. route ("/") def index (): return render_template ("index.html", site_name = 'myblog ')
The code in index.html is:
{Site_name }}The site name is {site_name }}
The render_template function of flask supports multiple parameters. The first parameter of the function is the template name, and several parameters can be provided afterwards, which are key-value pairs and provide data for the variables in the template. In this example, the value of myblog is provided for site_name, and {parameter name} is used in the template to represent a variable.
In this case, the output result of the browser input address is:
MyblogThis site is named myblog
The jinja2 template also provides variable filters, such as code:
{Site_name | upper }}The site name is {site_name }}
The output is as follows:
MYBLOGThis site is named myblog
Common filters are as follows:
Safe without escaping
Capital letters of capitalize
Lower to lowercase
Convert upper to uppercase
Trim space
Remove html tags from striptages
In addition, Jinja2 variables can be complex types, or even common methods of complex types, such:
{Site_name [2:]} The site name is {site_name }}
The output is as follows:
BlogThis site is named myblog
Control statement
The control statement is the basic function of a template. jinja2 also provides the following functions:
// Select {% if name = 'test' %}. this is the test {% else %} {name}. hello {% endif %} // Loop
{% For blog in blogs % }{{ blog. title }}{% endfor %}
In addition to these basic functions, the template also uses macro functions for the reuse of some code, such as writing the code below into the macros.html file.
{% macro render_title(blog)%}
{{blog.title}}{% endmacro%}
Then in the previous template:
{% import 'macros.html' as macros %}
{% for blog in blogs %} {{ macros.render_title(blog) }} {% endfor %}
The execution result is exactly the same as the previous one.
The basic template of the worker:
{% Block head %}
{% Block title % }{% endblock %}-blog