2. Flask learning template

Flask configures the Jinja2 template engine for you. Templates can be rendered using the render_template() method, simply by providing the template name and variables that need to be passed as parameters to the template.

  As for template rendering? Simply put, it renders "data" into your template. After reading the following chapter examples you will know.

  (In fact, the more recommended way is to separate the front and back ends. Flask templates are not recommended. The following is for learning reference only)

Template Basics

    Let's start with a simple template example:

1 from flask import Flask, render_template
2 
3 app  = Flask(__name__)
4
5 @app.route("/")
6 def template1():
7     return render_template("index.html")
8 if __name__=='__main__':
9     app.run()

Where should html template documents such as the above index.html be placed? Flask will look for templates in the templates folder (note the spelling). So, if your app is a module, the templates folder should be next to the module; if it's a package, it should be inside the package:

    Case 1: A module:
      /application.py
      /templates
          /xxx.html

    Case 2: A package:
      /application
        /__init__.py
        /templates

   Template rendering

    Here is just a brief introduction to template rendering, you can make full use of the power of Jinja2 template engine. For more information, see the official  Jinja2 template documentation  .

    Create a new flask.html, which is also placed in the templates folder. The body content is as follows:

    <body>
        <h1>{{ content }}</h1>
    </body>

    Among them, {{ content }} is the parameter that needs to be passed; the difference between the template file here and the HTML file is that a dynamic content placeholder composed of {{ ... }} is added to it.

    The same as the above example, create a new route, the code is as follows:

@app.route("/flask")
def template2():
    c = "flask"
    return render_template("flask.html",content=c)

    After the example runs, visiting localhost:5000/flask will return to display flask.html, where the content part will return the value you set in the code:

  template derivation

    Inside the template you can also access the request , session and g objects, as well as the get_flashed_messages() function.

    Templates are especially useful when inheritance is used, see the  template inheritance documentation for more information  . Simply put, template inheritance keeps specific elements (such as header, navigation, footer) consistent for each page.

    Auto-escaping is enabled by default. Therefore, if name contains HTML, it will be automatically escaped. If you can trust a variable and know it's safe HTML (eg the variable comes from a module that converts wiki markup to HTML), you can use the Markup class to mark it safe. Otherwise use the |safe filter in the template. See the Jinja 2 documentation for more examples.

 

This blog post only briefly introduces Flask templates. It is very convenient to use Flask templates for simple pages, but complicated ones are troublesome. As mentioned at the beginning, it is recommended to separate the front and back ends. Please refer to the Jinja 2 documentation for detailed template usage.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602656&siteId=291194637