Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Flask template

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use the Flask template, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. What is the template engine of Flask?

Flask's template engine uses jinja2, the official website:

Http://jinja.pocoo.org

Templates allow us to reuse code snippets in multiple places, making them ideal for dynamic HTML pages.

two。 How to use Flask's template engine?

First, continue with the contents of the previous chapter, create a new folder templates in the Flask_Blog folder:

Create a new layout.html page in the folder templates with the following contents:

Create a new home.html page in the folder templates with the following contents:

Create a new about.html page in the folder templates with the following contents:

Create a new folder static in the Flask_Blog folder and a new style file main.css in the static folder with the following contents:

Next, let's modify flaskblog.py:

From flask import Flask,render_tyemplate,url_forapp = Flask (_ _ name__) # define a list for foreground data display using posts = [{'author':' Liaobei Emperor', 'title':' blog first post', 'content':' blog first post', 'date_posted':' June 12, 2019'} {'author':' loves miracles', 'title':' blog post 2', 'content':' blog post 2', 'date_posted':' June 12, 2019'}] @ app.route ("/") @ app.route ("/ home") def home (): return render_template ('home.html') Posts=posts) @ app.route ("/ about") def about (): return render_template ('about.html', title=' about') if _ _ name__ ='_ _ main__': app.run (debug=True)

Then we open the command line cmd in the folder Flask_Blog and run python falskblog.py:

We visit the address http://127.0.0.1:5000/:

We continue to visit the address: http://127.0.0.1:5000/about:

Because the title parameter value is passed in the code, the foreground page receives and determines that the title has a value and displays the title content.

Thank you for reading this article carefully. I hope the article "how to use the Flask template" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report