Flask Learning configuration file loading and use of dynamic URLs

Source: Internet
Author: User

Seven lines of code to implement a flask app
fromimport= Flask(__name__)@app.route(‘/‘)def helloworld():    return‘helloworld‘if__name__==‘__main__‘:    app.run()

app.run()Suitable for commissioning only, not for production environments, production environments should start with Gunicorn and Uwsgi

Configuration Management
  • App. Config is an instance of the Flask.config.Config class that inherits from the Python built-in data structure dict, which you can add with Dict configuration
    1. app.config[' DEBUG ']= True
    2. Use the Update method
     App.config.update (Debug=  true , Secrey_key=   ' HELLOWORLD ' ,)  
  • General project configurations are uniformly placed in a single settings.py file that can centrally manage configuration items

    # settings.pyclass Develop():    =Trueclass Product():    =False== Product()
    # app.pyfromimport Flaskfromimport= Flask(__name__)app.config.from_object(product)
  • By loading the file name, you can learn
    • app.config.from_pyfile(‘settings.py‘,slient=True)
  • By loading the environment variables, you can understand
    • app.config.from_envvar(‘settings_path‘)
Configuration of dynamic URLs
    • The URL Translator Converter is defined in flask and requires the actual special field tag when used
    • <name>No parameter type specified or <converter_name:name> parameter type specified

      @app.route(‘/item/<id>/‘)def detali(id):returnid

Custom URL Converters

  • Inherits from Baseconverter

     From  werkzeug.routing import  baseconverterclass  Customconverter (baseconverter): def  __init__  ( Self , Url_map,regex): super  (customconverter, self ). __init__  (url_map) self . Regex =  regex  pre> 
  • Use

    fromimport= Flask(__name__)app.config.from_object(develop)app.url_map.converters[‘re‘=# 添加到converter列表中@app.route(‘/hello/<re("[1-9]{2}"):cus>/‘# re("regex") 定义匹配规则def custom(cus):    return cus

Flask Learning configuration file loading and use of dynamic URLs

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.