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 in python

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to use flask in python, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. File introduction, which is introduced through import, has the following ways

From appon.base.baseController import * import cgiimport appon.controller.loginController as LoginC

2. Session and redirect used in python need to be introduced through import.

3 、 flask

App = Flask (_ _ name__,template_folder='appon/view/',static_folder= "", static_url_path= "")

For the instantiated object, the default value of the first parameter is generally not modified. Template_folder represents the template path, which is at the same level of the entry file by default. You can modify the default template path by passing parameters.

App.debug = True

Set the project to debug mode to facilitate developers to debug during development. Note: the first letter of bool must be capitalized.

App.secret_key = secret_key

This parameter protects session data. If it is not set, an error will be reported.

Redirect (url_for ('home'))

Jump to the specified path

Render_template ('login/index.html')

Template rendering

Load the general template file in the template page through extends

When extends introduces a file, a html can only use {% extends "public/header.html"%} once.

Load the general template file in the template page through include

When include introduces a file, a html can use {% include "public/header.html"%} multiple times

Template reads session data

{{session ['username']}}

4. Connect MySQL

Import MySQL file

Import mysql.connectorimport mysql

Introduction of MySQL profile

From appon.config.db import *

Create a MySQL connection

# create a database connection conn = mysql.connector.connect (* * dbInfo) # use the cursor () method to create a cursor object cursorcursor = conn.cursor (dictionary = True)

Dictionary = True, this parameter converts the MySQL data into KMurv form

The specific execution process of sql can be found in the source file. It should be noted that after each connection is created, the sql task needs to be executed in turn: sql submission, cursor closure, connection closure, and then return the database results to the corresponding request method.

5. Receive form form data

Username = request.form ['username'] if username = = False: return "username is empty"

Received via request.form, each index is the field name corresponding to the form form, and returns False if it does not exist

6. Session settings

# you can set the session validity period session.permanent = Truesession ["username"] = usernamesession ["userid"] = adminInfo ['id']

Read session

# read sessionsession.get ('username')

7. Routing

@ app.route ('/ login') def login (): return render_template ('login/index.html')

Default in localhost:5000/login, default get access

@ app.route ('/ doLogin',methods= ['GET',' POST']) def doLogin (): does return LoginC.doLogin () help you after reading the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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