Use of render_template in flask and inheritance of templates

Source: Internet
Author: User
Tags inheritance

This is the legendary Mvc:model-view-controller, Chinese name "model-View-controller".
Python's function of handling URLs is C:controller,controller is responsible for business logic, such as checking the existence of user names, removing user information and so on;
The template that contains the variable {{name}} is the v:view,view responsible for displaying the logic, and by simply replacing some variables, the view ultimately outputs the HTML that the user sees.
Where is the model in MVC? The model is used to pass to the view, so that when the view replaces the variable, it can take the corresponding data out of the model.
Template:
The location of the template is placed under the Templates folder, usually the HTML file, we change the index.html to the following style

 

Where: {{}} indicates that this is a variable that can be adjusted according to the parameters given by the user at the module-side

The following program calls the Render_template template

From the flask importrender_template from
app import app
@app. Route ('/')
@app. Route ('/index ')
def index ( ):
    user = {' nickname ': ' Miguel '} # Fake user
    return render_template ("index.html",
        title = ' Home ',
        user = User) #这里模块里的第一个user指的是html里面的变量user, and the second user refers to the variable user in the function index

Frankly speaking, in fact, the function of Render_template is to introduce index.html, and then modify the rendering of HTML according to the parameters passed in later.
Then, the render_template template is actually accept the control statement, the modified index.html as follows:

 

Let's look at a modified version of views.py and index.html for a loop statement.

def index ():
    user = {' nickname ': ' Miguel '} # Fake user
    posts = [# Fake array of posts
        {
            ' author ': {' nickn ' Ame ': ' John '},
            ' body ': ' Beautiful day in portland! '
        },
        {'
            author ': {' nickname ': ' Susan '},
            ' body ': ' The Avengers movie is so cool! '
        }
    ]
    Return Render_template ("index.html",
        title = ' Home ',
        user = user,
        posts = posts)

Let's look at a modified version of views.py and index.html for a loop statement.

def index ():
    user = {' nickname ': ' Miguel '} # Fake user
    posts = [# Fake array of posts
        {
            ' author ': {' nickn ' Ame ': ' John '},
            ' body ': ' Beautiful day in portland! '
        },
        {'
            author ': {' nickname ': ' Susan '},
            ' body ': ' The Avengers movie is so cool! '
        }
    ]
    Return Render_template ("index.html",
        title = ' Home ',
        user = user,
        posts = posts)

Note that the visits here are inherited by points, such as Post.author.nickname

Finally, if in the future development, the template becomes very much, but each template, there are fixed items can not change, how to operate it ....
For example, there is a button on the top of the page, there are other pages, it is not possible to write on each page
So, we put the same part in the base.html this basic template, through the {% block content%} this interface, and base.html to do the link

 
{% extends "base.html"%}                                       #通过这句话, to carry out inheritance hooks, and principal base.html to link
{% block content%}

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.