Intro
If you encounter a must provide Secret_key to use CSRF error alert, the reason is not set Secret_key, add the code
app.config[' secret_key ']= ' xxx '
Secret_key best not to write in the code.
It is best to set up a config.py file from which to read the content
config.py
csrf_enabled = Truesecret_key = ' you-will-never-guess '
app.py
App.config.from_object (' config ')
This will prevent the CSRF.
Well, then let's talk about this secret_key:
Flask a simple way to generate Secret_key (key)
Secret_key is one of the more important configuration values in flask. This paper introduces a relatively simple method of generating secret_key.
Sessions, cookies, and some third-party extensions will all use the Secret_key value, which is a more important configuration value and should be set to a hard-to-guess value, preferably at random.
The stochastic problem is that it is difficult to judge what is true random. A key should be random enough. Your operating system can generate beautiful random values based on a random password generator, which can be used to make a key:
>>> import os>>> os.urandom ' \xca\x0c\x86\x04\x98@\x02b\x1b7\x8c\x88]\x1b\xd7 ' +\xe6px@\xc3#\ \'
Copy and paste this value into your code, and you're done with the key.
Setup method:
App = Flask (__name__) app.config[' secret_key '] = ' \xca\x0c\x86\x04\x98@\x02b\x1b7\x8c\x88]\x1b\xd7 ' +\xe6px@\xc3#\\ ' # Orapp.secret_key = ' \xca\x0c\x86\x04\x98@\x02b\x1b7\x8c\x88]\x1b\xd7 ' +\xe6px@\xc3#\\ ' # orapp.config.update ( secret_key= ' \xca\x0c\x86\x04\x98@\x02b\x1b7\x8c\x88]\x1b\xd7 ' +\xe6px@\xc3#\\ ')