Flask tutorial (II), templates template Quick Start

Source: Internet
Author: User

Flask tutorial (II), templates template Quick Start
Flask tutorial (II), templates template quick start, if you have done the content in the previous chapter. The file structure should look like the following:

    flaskstudy\      app\        static\        templates\        __init__.py        views.py      tmp\      run.py
I have also tested it in the previous section. They all work. The content in this section is a template. Why? The answer is required! Business separation, so that the front-end is engaged in front-end, the background is engaged in the background, all the pages are written in the code like what happened. Let's take a look at the following example:
From app import app@app.route ('/') @ app. route ('/Index') def index (): user = {'nickname ': 'maobao' }# fake user return ''' There are a lot of waves, but this is still a small number. If there are thousands of lines of UI, the programmer may have to spray blood directly. It is unimaginable to have no templates. It is estimated that no one has used python to develop websites. In critical moments, templates is used to save fire. The preceding format is rewritten as follows: Create a template file: app/templates/index.html:
The difference is that two pairs of braces {} are used to store the data content. In essence, it should be a replacement. Of course, views must also be modified as follows:

From flask import render_templatefrom app import app@app.route ('/') @ app. route ('/Index') def index (): user = {'nickname': 'day'} # fake user return render_template('index.html', title = 'home', user = user)
At the same time, we have to introduce a new Flask module to render the template. render_template is used to replace the parameter. Render_template will summon X-Dragon Jinja2 template rendering engine to replace {} Jinja2 Official Website: http://jinja.pocoo.org/Jinja2 is Python next widely used template engine, his design idea comes from Django template engine, it also extends its syntax and a series of powerful functions. The most significant one is the addition of the sandbox execution function and the optional automatic escape function, which is very important for the security of most applications. It is unicode-based and can run in Versions later than python2.4, including python3. At the same time, jinja2 also supports judgment statements:

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312" >{% if title %}
 <title>{{ title }} - wetucao</title>    {% else %}    <title>Welcome to wetucao</title>    {% endif %}  

   
Now the guy upstairs is a little smart and can tell different situations. But what about many people? Cycle in the template: many friends may need to be displayed. What should we do? Here, the author is the author, and the body field is the input for submission.
Def index (): user = {'nickname': 'Big cousin '} # fake user posts = [# fake array of posts {'author': {'nickname ': 'coward '}, 'body': 'beauul ul day in Portland! '}, {'Author': {'nickname': 'Big health'}, 'body': 'avengers movie was so cool! '}] Return render_template ("index.html", title = 'home', user = user, posts = posts)
Of course, the template file must be modified accordingly, mainly by adding for post in posts:
Simple? So easy! Mom no longer needs to worry that you don't take any medicine. In fact, the three procedures, sequence, branch, and loop, all of which have been controlled are included. They are not written for no reason.

The inheritance of templates generally requires some public modules for websites, such as navigation and foot modules. At this time, if each template is pasted and copied repeatedly, it will be very inefficient and is not conducive to maintenance (a change should be done repeatedly). Therefore, inheritance is very useful, templates can also be inherited. Everyone knows the truth, so I won't be talking about it here. Let's talk about how it works in flask. First, create a base Folder:
<Html> What is the difference between this and the previous one? The block control module is used to store the content of our modules. Then the corresponding index.html is changed:
{% extends "base.html" %}{% block content %}    That is quite convenient!

Big cousin:

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.