tutorial on using HTML templates in Python

Source: Internet
Author: User
Tags button type

This article mainly introduces the use of HTML templates in Python tutorial, HTML template is a Python's major framework of a basic function, the need for friends can refer to the

The web framework saved us from the WSGI. Now, we just need to write the function continuously, with the URL, we can continue the development of the Web App.

However, Web apps are more than just processing logic, and the pages that are displayed to users are also important. To return a string containing HTML in a function, simple pages are OK, but think about more than 6,000 lines of HTML on Sina's home page, are you sure you can write it correctly in a Python string? I can't do it anyway.

As the saying goes, Python engineers who don't know the front end are not good product managers. Students with web development experience understand that the most complex part of web apps is on the HTML page. HTML is not only correct, but also through CSS beautification, plus a complex JavaScript script to achieve a variety of interactive and animation effects. In short, it is difficult to generate HTML pages.

Because it is unrealistic to spell strings in Python code, template technology appears.

With templates, we need to prepare an HTML document that is not normal HTML, but embeds variables and instructions, and then, based on our incoming data, replaces it, gets the final HTML, and sends it to the user:

This is the legendary Mvc:model-view-controller, the Chinese name "model-View-controller".

Python's function of dealing with URLs is that C:controller,controller is responsible for business logic, such as checking the existence of user names, taking out user information, and so on;

The template that contains the variable {{name}} is V:view,view is responsible for displaying the logic, by simply replacing some variables, the View ultimately outputs the HTML that the user sees.

Where is the model in MVC? Model is used to pass the view, so that when the view replaces the variable, it can retrieve the corresponding data from model.

In the example above, model is a dict:

{' name ': ' Michael '}

Just because Python supports keyword parameters, many web frameworks allow you to pass in keyword parameters, and then assemble a dict inside the frame as model.

Now, we'll rewrite the last direct output string as an example of HTML with the high-end atmospheric MVC pattern:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 From flask import flask, request, render_template app = Flask (__name__) @app. Route ('/', methods=[' get ', ' POST ']) def h ome (): Return render_template (' home.html ') @app. Route ('/signin ', methods=[' Get ']) def signin_form (): Return Render_ Template (' form.html ') @app. Route ('/signin ', methods=[' POST ') def signin (): username = request.form[' username '] Password = request.form[' password '] if username== ' admin ' and password== ' password ': Return render_template (' Signin-ok.html ', Username=username return render_template (' form.html ', message= ' bad username or password ', username= username) If __name__ = = ' __main__ ': App.run ()

Flask uses the render_template () function to implement the rendering of the template. Like the web framework, Python templates are also available in many ways. The flask default supported template is JINJA2, so we first install JINJA2 directly:


1 $ Easy_install JINJA2

Then, start writing the jinja2 template:


1 Home.html

The template used to display the first page:


1 2 3 4 5 6 7 8 9 10 11

Template used to display the login form:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18

Template for Successful login:


1 2 3 4 5 6 7 8

What about the failed login template? We added a bit of form.html to the decision to reuse form.html as a login failure template.

Finally, be sure to put the template in the correct templates directory, templates and app.py in the same level directory:

Start the Python app.py to see the page effects using the template:

With MVC, we deal with M:model and C:controller in Python code, and V:view is handled through templates, so that we can successfully isolate the Python code from the HTML code.

Another great benefit of using templates is that templates are easy to change, and when you're finished saving, refreshing your browser to see the latest results is too important for front-end engineers who debug HTML, CSS, and JavaScript.

In the JINJA2 template, we use the {{name}}} to represent a variable that needs to be replaced. Most of the time, you also need to loop, conditional judgment and other instruction statements, in the JINJA2, with {% ...%} to represent instructions.

For example, loop output page number:


1 2 3 {% for I in page_list%} <a href= '/page/{{i}} ' >{{I}}</a> {% endfor%}

If Page_list is a list:[1, 2, 3, 4, 5], the template above will output 5 hyperlinks.

In addition to JINJA2, the common templates are:

Mako: A template with <% ...%> and ${xxx};

Cheetah: It is also a template with <% ...%> and ${xxx};

Django:django is a one-stop frame with a built-in template with {% ...%} and {{XXX}}.


With MVC, we split the Python code and the HTML code. The HTML code is all in the template and is more efficient to write.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.