Flask the most storming-with Dragonfire learn flask-eighth instantiate the parameters of flask and the configuration of the app

Source: Internet
Author: User

Flask is a very flexible and short-witted web framework, so where does flexibility manifest itself?

There is a magical thing called flask configuration, how to use this thing? What kind of convenience can it bring us?

First show:

 from Import  = Flask (__name__)  #  type:flaskapp.config["DEBUG" ] = True

This sentence app.config["DEBUG"] = True can be achieved by the function can stimulate the

Automatically restarts the Flask program (App.run) as soon as the code changes

The information printed on the console is very comprehensive

The above two functions are the legendary debug mode (debug mode)

Flask's configuration is to add a key-value pair in app. Config, but the key you put in must be in config, and if it doesn't, it will be useless by default.

How many useful keys are there in config?

{    'DEBUG': False,#whether to turn on debug mode    'Testing': False,#whether to turn on test mode    'propagate_exceptions': None,#exception propagation (whether log is printed on the console) automatically true when debug or testing is turned on    'preserve_context_on_exception': None,#one or two words are not clear, generally do not use it    'Secret_key': None,#have encountered before, when the session is enabled, must have it    'Permanent_session_lifetime': 31,#days, Session life cycle (day) default 31 days    'Use_x_sendfile': False,#whether to discard X_sendfile    'Logger_name': None,#the name of the logger    'Logger_handler_policy':' always',    'server_name': None,#service access domain name    'Application_root': None,#the full path of the project    'Session_cookie_name':'Session',#storing the name of the session encrypted string in a cookie    'Session_cookie_domain': None,#under which domain name the session record is generated in the cookie    'Session_cookie_path': None,#Path to Cookies    'session_cookie_httponly': True,#controls whether the cookie should be set to the HTTPONLY flag,    'session_cookie_secure': False,#controls whether the cookie should be set to a security flag    'session_refresh_each_request': True,#This flag controls how permanent sessions are refreshed    'Max_content_length': None,#if set to the number of bytes, Flask rejects a request entry with a content length greater than this value and returns a 413 status code    'Send_file_max_age_default': 12,#hours maximum age for default cache control    'trap_bad_request_errors': False,#If this value is set to True, flask does not perform the error handling of HTTP exceptions, but instead treats other exceptions as    #Let it bubble out through the exception stack. This is useful for scary debugging scenarios that need to find the source of the HTTP exception.     'trap_http_exceptions': False,#WERKZEUG internal data structures that handle the specific data in the request will throw a special key errors that is also the "wrong request" exception.     #Similarly, in order to remain consistent, many operations can explicitly throw badrequest exceptions.     #because in debugging, you want to pinpoint the cause of the exception, this setting is used to debug in these situations.     #If this value is set to True, you will only get regular backtracking.     'explain_template_loading': False,'Preferred_url_scheme':'http',#This value will be used if no URL pattern is available when generating the URL    'Json_as_ascii': True,#By default, Flask uses ASCII encoding to serialize objects. If this value is set to False,    #flask does not encode it as ASCII and outputs it as-is, returning its Unicode string.     #For example, Jsonfiy will automatically use Utf-8 to encode it before transmitting.     'Json_sort_keys': True,#by default, Flask serializes a JSON object in the order it is ordered by its key.     #This is done to ensure that the order of the keys is not affected by the hash seed of the dictionary, so that the returned values are consistent every time and do not result in unnecessary additional HTTP caches.     #You can override the default action by modifying the value of this configuration. But this is not recommended because this default behavior may give you an improvement in the cost of performance.     'Jsonify_prettyprint_regular': True,'Jsonify_mimetype':'Application/json',    'Templates_auto_reload': None,}

These keys can be rewritten, of course, they also have default values exist, if there is no special case, do not overwrite its default value

The configuration is modified in approximately two ways

1. Modify the app. config directly

app.config["DEBUG"] = True

2. Importing by using a class

First you have to have a settings.py file.

class flasksetting:     = True    "dragonfire"

And then we can write that in flask's boot file.

 from Import  = Flask (__name__)  #  type:flaskapp.config.from_object ("  Settings. Flasksetting")

This is called Class import configuration

This is a configuration for an already instantiated app

So when the flask instantiation, what are the arguments passed?

In fact, it can be understood as the initial configuration of the flask instance, which is a very good understanding of the parameters, note that the keyword is very very very good understanding

Static_folder ='Static',#the path to the static file directory defaults to the static directory in the current projectStatic_host = None,#The host address used by the remote static file, which is empty by defaultStatic_url_path = None,#the URL path of the static file directory is not written by default when the remote static file is reused with the same name as Static_folder#host_matching If the host host bit match is turned on, is to be used with static_host and must be assigned true if Static_host is configured#to illustrate, @app. Route ("/", host= "localhost:5000") it has to be written like this.#host= "localhost:5000" cannot pass the current route if the host header is not localhost:5000host_matching = False,#if it's not a special need, use it sparingly, otherwise all route will need to host= "" parametersubdomain_matching = False,#In theory it is used to restrict server_name subdomains, but there is no sense of the difference .Template_folder ='Templates'  #template Templates directory, default templates directory in the current projectInstance_path = None,#path to another flask instanceInstance_relative_config = False#whether to load the configuration of another instanceRoot_path = None#absolute path to the directory where the main module resides, default project directory

In this, the parameters we use are

' Static ',  # The path to the static file directory defaults to the static directory in the current project Static_url_path = None,  #  The URL path of the static file directory is not written by default to be the same name as Static_folder, and the remote static file is reused 'templates'  # template Templates directory, default templates directory in the current project

Just remember that. In a general project, only these parameters are modified

Eighth article, end

Flask the most storming-with Dragonfire learn flask-eighth instantiate the parameters of flask and the configuration of the app

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.