当前位置:网站首页>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 :

 

 

原网站

版权声明
本文为[liu_ xzhen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230401246254.html