Flask Learning Notes (1) method of--flask template parameter transfer value _flask parameter

Source: Internet
Author: User
Tags lowercase
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.