Flask from Basics to Combat (1) has implemented the first Falsk hello World program successfully access, this time to talk about the other basics.

Program loading configuration

When the flask is running, you can configure the Debug mode for the flask, configure the database connection address, and so on

Loading from a configuration object (common)


Load from a configuration file




The configuration file is read using app.config.get().

The parameters of the app. The run ()

You can specify the HOST IP address, port, and whether to enable debugging mode

App. run(host=”0.0.0.0″, port=5000, debug = True)

Basic definition of routes


With a browser access, you can see the return value of the view function


Route parameter transmission

Arguments passed by route are treated as strings by default. You can also specify the type of the argument, which will be explained later. Here’s how to pass the argument.


The request result is as follows


Specify request mode

In Flask, a route is defined. The default request is:

    GET

The OPTIONS (bring)

The HEAD (bring)

If you want to add a requester trial, you can specify the following:


You can find this by printing app.url_map


Common logic for views

Returns a json

The first way:


Result after request:


The second way:


Request result:


You may think that there is no difference between the two types of request, which return a JSON string, but there is a difference. Let’s take a look at the response headers for the two types of request

The first returns a response header in json data format, as follows


Next is the second response header that returns JSON data, as follows:


You can see that the content type returned by the second response header has changed

Redirect: url_for

Redirect to the view function you wrote

You can directly enter your own URL path


You can also use url_for to generate the URL for the specified view function


Custom status code

In Flask, you can easily return custom status codes to implement non-HTTP status codes

The second value returned by the following view function is the status code 666: