In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the Flask template in Python? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
In a normal Web program, visiting an address usually returns a HTML page containing all kinds of information. Because our program is dynamic, some information in the page needs to be adjusted according to different situations, such as displaying different information for logged-in and non-logged-in users, so the page needs to be dynamically generated according to the program logic when the user visits.
We call HTML or other formatted text containing variables and operational logic templates, and the process of performing these variable substitutions and logical calculations is called rendering (template rendering engine-Jinja2).
By default, Flask looks for templates in the templates folder of the module's sibling directory where the program instance is located.
Basic template syntax
In the template, you need to add specific delimiters to mark Jinja2 statements and variables
Here are three commonly used delimiters:
1. {{… }} is used to mark variables.
2. {% … %} is used to mark statements, such as if statements, for statements, etc.
3. {#... #} used to write comments.
The variables used in the template need to be passed in when rendering.
Write a home page template
Let's first create a movie.html file in the templates directory as the home page template. The home page needs to display a list of movie entries and personal information, and the code is as follows:
Movie list {# use the length filter to get the length of the movies variable #}
{{movies | length}} Titles
{% for movie in movies%} {# iterative movies variable #} {{movie.title}}-{{movie.year}} {# equals movie ['title'] #} {% endfor%} {# end for statement #} with endfor tag
To facilitate variable processing, Jinja2 provides some filters (similar to the filters in the Vue.js framework), in the following syntax:
{{variable | filter}}
On the left is the variable and on the right is the filter name. For example, the template above uses the length filter to get the length of the movies, similar to the len () function in Python.
Prepare virtual data
In order to simulate page rendering, we need to create some virtual data to populate the page content, where virtual data is defined in template.py.
Movies = [{'title':' Changjin Lake', 'year':' 2021'}, {'title':' send you a little red flower', 'year':' 2021'}] render home page template
You can render the template using the render_template () function, which must be passed in as the template file name (relative to the file path of the templates root directory), in this case, 'movie.html'. In order for the template to render correctly, we also pass the variables used inside the template into this function through keyword parameters, as shown below:
From flask import Flask, render_template app = Flask (_ _ name__) @ app.route ('/') def movie (): return render_template ('movie.html', movies=movies) if _ _ name__ = = "_ _ main__": app.run ()
Among the keyword arguments passed in the render_template () function, the movies on the left is the variable name used in the template, and the movies on the right is the actual object that the variable points to. Here the movies of the template is a list, but you can use not only this Python data structure in the template, you can also pass in strings, tuples, dictionaries, functions, and so on.
When called, the render_template () function recognizes and executes all the Jinja2 statements in "movie.html" and returns the rendered template content. On the returned page, the variable is replaced with the actual value (including delimiters), and the statement (and delimiters) is removed after execution (and comments are also removed).
After reading the above, have you mastered what the Flask template in Python is? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.