当前位置:网站首页>Flask learning notes
Flask learning notes
2022-07-23 10:32:00 【liu_ xzhen】
- 2-6 flask Minimum prototypes and uniqueness URL principle ; Add in... When registering routes /, Avoid typing more in the browser / Report the wrong question
@app.route('/index/', methods=['POST'])
def func():
pass
# @app.route('/index', methods=['POST'])
# If it is written in this way , Input more in the browser / Will report a mistake The principle is flask Using redirection

- 2-7 Another way to register a route
Start the service automatically after modifying the code , Turn on debug After the model , If an error , Will print in detail on the browser
# Use flask Debugging mode of , When a file change is detected, the service will automatically restart
app.run(debug=True)
# Another route registration , The decorator method is recommended . If a class based view , The following methods must be used
app.add_url_rule('/index/',endpoint='index',view_func=index)python Decorators don't understand , Know in time ,python Distinctive coding method , It can solve many problems such as coupling . Decorator python Grammatical basis is very important .
- 2-8 app.run Relevant parameters and flask The configuration file
# If you want to let the Internet access , add to host Parameter configuration .
# host You can also write to this machine ip Address , Shortcomings need to be modified after changing the server host Parameters .
# Appoint 0.0.0.0 Automatically get local ip
app.run(host="0.0.0.0", port=81, debug=True)
# flask Load profile , Create a new... Under the same level directory of the startup file config.py file , Defining variables .
# Be careful :from_object Method parameter names must be all uppercase !!!
# How to use the startup file , Load the configuration file first , Reuse .
app.config.from_object('config')
print(app.config['DEBUG'])- 2-9 You don't really understand if __name__ The role of
# This startup method is limited to the development environment , Deployment in a production environment requires nginx+uwsgi
# In a real production environment app.run It's not going to be implemented
if __name__ == '__main__':
app.run(host=app.config['URL'], port=app.config['PORT'], debug=app.config['DEBUG'])- 2-10 The response object :Response
from flask import make_response

Reference resources :https://www.jb51.net/article/217493.htm
- 3-3 Search for keywords
# Route through <> Pass parameters
# Define routes , The parameter name passed is name, Therefore, you need to define parameters with the same name in the formal parameters of the function
@app.route('/user/<name>')
def get_name(name):
return " The received name is : %s" % name
@app.route('/news/<int:id>')
def list_news(id):
return " The received id by : %s" % id
Write if When judging , It is very likely to meet the conditions and put it in front , Improve code execution efficiency .
No matter how you write the code , At least ensure that the code in the view function is concise and easy to understand . Because the view function starts a web The starting point of the project .
Too many comments are not good
- 3-5 requests send out http Simplified means of request and code

simplify if else Code for :1. Binocular expression ;2. Clever use if+return( The code specification suggests one function per return)3.if else Extract another function from the more complex code in ,for Cycle also applies .
- 3-6 requests vs urllib
Define the methods of the class , Static methods :


边栏推荐
- Kingbasees SQL language reference manual of Jincang database (8. Function (4))
- 赛尔运维:高校IT运维服务新样本
- 金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(一))
- Moment get week, month, quarter, year
- 仅用5000行代码,在V853上AI渲染出一亿幅山水画
- Anaconda source change and opencv installation
- Kingbasees SQL language reference manual of Jincang database (8. Function (III))
- Kingbasees SQL language reference manual of Jincang database (8. Function (7))
- SAP 批导模板(WBS批导为例)
- EasyCVR平台CGO回放回调参数缺失导致设备录像无法播放,该如何解决?
猜你喜欢

隐适美invisAlign口扫转诊方式(导出口扫数据+线上问诊)

Redis replication cluster setup

禅道的甘特图功能是什么

Practice of online problem feedback module (11): realize image download function

"Lost wake up problem" in multithreading | why do wait() and notify() need to be used with the synchronized keyword?

performance介绍

浏览器怎么导入导出|删除书签,方法步骤来咯

CV (3)- CNNs

2022/7/21

New file / filter / folder in VS
随机推荐
1. Assignment statement
2022/7/21
智慧园区的核心本质是什么?
Redis安装
MySQL基础篇(运算符、排序分页、多表查询、函数)
金仓数据库 KingbaseES SQL 语言参考手册 (4. 伪列)
Flask学习笔记
EasyCVR新版本(v2.5.0)目录分级功能如何使用?
IDEA 集成 Sonar 完整流程
New file / filter / folder in VS
SSH supermarket inventory management system
performance介绍
什么是即时通讯?即时通讯的发展
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(六))
浏览安全怎么提升?教你设置安全浏览器信任站点
EasyCVR平台CGO回放回调参数缺失导致设备录像无法播放,该如何解决?
Kingbasees SQL language reference manual of Jincang database (8. Function (III))
Redis installation
What is instant messaging? Development of instant messaging
赛尔运维:高校IT运维服务新样本