Teach you examples of using template engine and form plug-ins (python)

Source: Internet
Author: User
After the first chapter of the content, has been able to make some simple page, first in this way to do a login page, first to create a login routing method:

@app. Route ("/login", methods=["GET"] def login (): html= "<form method= ' post ' >" \ "<table>" \ "<tr> <td> Please enter the username </td><td><input type= ' text ' name= ' username '/></td></tr> "\" <tr ><td> Please enter the password </td><td><input type= ' password ' name= ' password '/></td></tr> ' \ ' <tr><td><input type= ' submit ' value= ' login '/></td></tr> ' \ ' </table> ' \ ' </post > "Return HTML

This page is returned with a simple login page that shows the following results:

Then there is a login post back page:

@app. Route ("/login", methods=["POST"]) def loginpost (): Username=request.form.get ("username", "") password= Request.form.get ("Password", "") If username== "test" and password== "123": Return "Login succeeded" Else:return "Login Failed"

Show login success after entering test and 123

The function of course is implemented, but in other aspects, it is difficult to say that it is a practical application, even if not to consider the JS script and CSS style sheet, from simple maintenance, is a nightmare, such as adding a verification code box, I think no one think it is a pleasant job.

So, first of all, we're going to separate the page HTML part, which flask provides the JINJA2 template engine to implement.

JINJA2 template engine also conforms to flask the most basic convention, that is, many configurations have a basic default value, relative JINJA2, there is one of the most important default value, that is, the template file is placed in the Templates folder, although this folder can be customized, but for the time being, It is sufficient to use the default values.

OK, first create the Templates folder in the Pycharm project root directory, and then create the login.html file within the folder, of course the directory structure is as follows:

Enter the following code in the login.html:

<! DOCTYPE html>

The code is very simple, also the name does not carry on the style aspect beautification, actually at present for me, only because has the intelligence perception, has the sufficient reason to use the template, then, the default.py login method modifies the code to be:

From flask import Render_template #头部, introducing template rendering Method @app.route ("/login", methods=["GET") def login (): Return render_template ("/login.html")  #渲染模板, the login.html file under the Templates folder is found by default

Because the code inside the HTML template and directly in the py file directly in the same, so refresh the page at this time, the display effect and just the same, although the display effect is not significantly improved, but at this time if you modify an element in the HTML, it will be much easier.

About the JINJA2 template engine also supports some of the more powerful features, such as using index to make some notes:

Basic usage

Modify some of the code in default.py to:

From flask import render_template #页头, import render function @app.route ("/") def index (): Return Render_template ("index.html", site_name = ' MyBlog ')

The code in Index.html is:

<! DOCTYPE html>

The render_template function of flask supports multiple parameters, where the first parameter of the function is the template name, which can then provide several parameters, all of which are key-value pairs, providing data for the variables in the template. In this example, the value of MyBlog is provided for site_name, while the template uses {{parameter name}} to represent a variable

At this point the browser input address output is:

<! DOCTYPE html>

The JINJA2 template also provides a number of variable filters, such as code:

<! DOCTYPE html>

At this point the output is:

<! DOCTYPE html>

Common filters are as follows

Safe does not escape
Capitalize first Letter Capital
Lower convert to lowercase
Upper Convert to uppercase
Trim to close the space
Striptages Removing HTML tags

In addition, JINJA2 variables can be complex types, and can even use some of the most common methods of complex types, such as:

<! DOCTYPE html>

At this point the output is:

<! DOCTYPE html>

Control statements

The control statement is the basic function of a template, and the same JINJA2 provides the appropriate functionality:

Select {% if name== ' test '%} This is the test {% Else%} {{name}}, hello {% endif%}//loop <ul> {% for blog in blogs%} {{Blog.title}} {% E Ndfor%}</ul>

In addition to these basic usages, the template provides macro functionality for some code reuse, such as writing the following code into a macros.html file

{% macro render_title (blog)%} <li>{{blog.title}}</li>{% endmacro%}

Then in the previous template:

{% import ' macros.html ' as macros%}<ul> {% for blog in blogs%} {{macros.render_title (blog)}} {% endfor%}</ul >

The execution results are exactly the same as before

JINJA2 also provides a more powerful feature, template inheritance, which feels a bit like the Java Sitemesh Framework, which first needs to create a base.html base template:

<! DOCTYPE html>

Where the meaning of the block tag can be modified in the sub-template, specific to this example, the modifiable part is head,title,body. The following is the child template code:

{% extends "base.html"%} {% block title%} {{site_name[2:]}} {% Endblock%} {% block head%} {{super ()}} {% Endblock%} {% block body%} 

At this point the execution result remains:

<! DOCTYPE html>

Now with the template engine, anyway, just on the page layer, it is easy to make some good features, but it is clear that the current interface is not very beautiful, the next chapter will now mainstream front-end framework bootstrap and flask framework to integrate.

Related Article

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.