[Flask] Flask template

Flask template

In the previous example, the main function of the view function is to generate a response to the request, which is the simplest request. In fact, the view function has two functions:

  • Processing business logic
  • Return response content

In large-scale applications, putting business logic and performance content together will increase code complexity and maintenance costs.

  • A template is actually a file containing response text, where a placeholder (variable) is used to represent the dynamic part, telling the template engine that its specific value needs to be obtained from the data used
  • Replace variables with real values ​​and return the final string. This process is called'rendering'
  • Flask uses Jinja2, a template engine to render templates

Benefits of using templates

  • View functions are only responsible for business logic and data processing (business logic aspects)
  • The template takes the data results of the view function for display (trying to display aspects)
  • Clear code structure and low coupling

Basic use of templates

Create a templates folder under the project to store all template files, and create a template file html file hello.html in the directory 

Sample code:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    my_int = 18
    my_str = 'curry'
    my_list = [1, 5, 4, 3, 2]
    my_dict = {
        'name': 'durant',
        'age': 28
    }

    # render_template方法:渲染模板
    # 参数1: 模板名称  参数n: 传到模板里的数据
    return render_template('hello.html',
                           my_int=my_int,
                           my_str=my_str,
                           my_list=my_list,
                           my_dict=my_dict)


if __name__ == '__main__':
    app.run(debug=True)

Create a new folder templates under the folder where py is located, and then create a new hello.html in templates 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>

<body>
  <h2>我是模板</h2>
  {
   
   { my_int }}
  <br>
  {
   
   { my_str }}
  <br>
  {
   
   { my_list }}
  <br>
  {
   
   { my_dict }}
  <hr>
  <h2>模板的list数据获取</h2>
  <hr>
  {
   
   { my_list[0] }}
  <br>
  {
   
   { my_list.1 }}
  <hr>
  <h2>字典数据获取</h2>
  <hr>
  {
   
   { my_dict['name'] }}
  <br>
  {
   
   { my_dict.age }}
  <hr>
  <h2>算术运算</h2>
  <br>
  {
   
   { my_list.0 + 10 }}
  <br>
  {
   
   { my_list[0] + my_list.1 }}
</body>

</html>

Visit htpp://127.0.0.1:5000/ to run the test 

use

  • { {}} to represent the variable name, this { {}} syntax is called variable code block
<h1>{
   
   {post.title}}</h1>

The variable code block in the Jinja2 template can be any Python type or object, as long as it can be converted into a string by Python's str() method. For example, an element in a dictionary or list can be displayed in the following way

{
   
   {your_dict['key']}}
{
   
   {your_list[0]}}
  • The control code block defined by {%%} can realize some language-level functions, such as loop statements
{% if user %}
    {
   
   { user }}
{% else %}
    hello!
<ul>
    {% for index in indexs %}
    <li> {
   
   { index }} </li>
    {% endfor %}
</ul>

Comment

  • Use {##} to comment, the content of the comment will not be rendered in html
{#{
   
   { name }}#}

 

 

Guess you like

Origin blog.csdn.net/u013066730/article/details/108356847