当前位置:网站首页>Flask framework to study: the debug and configuration items

Flask framework to study: the debug and configuration items

2022-08-11 05:45:00 weixin_42576837

Enable debug mode

  1. Pass parameters in app.run(), use debug = True to enable:
 app.run(debug=True)
  1. Set the configuration item and open it in the form of configuration parameter:
app = Flask(__name__)app.config['DEBUG'] = True

Open in Edit Configurations set in 3.pycharm:
InInsert image description here
insert image description here

Note: You can only see FLASK_DEBUG if the new Flask project is created here. If it is a new python project, this option is not available.

There are several pits here: Here my file name is: debug mode.py
But if you have not executed this file, this location will not display the current file:
insert image description here
My understanding is that you have to do it once, and then you will be in Edit Configurations generates a configuration item information of the current file, and then you edit the configuration information and set FLASK_DEBUG to select.

Then, when executing, you cannot directly right-click to execute
insert image description here
There is already a configuration item of debug mode.py in the above configuration information, you have already set it up, if you right-click to run now, note: it writes debug mode.py(1), this will generate another configuration item of debug mode.py(1), I don't understand why there is one more (1) comes out, but if you right-click to run, the debug mode is still not set.

The correct way to run is to click the run button next to it after Edit Configurations is set.
insert image description here
That's fine.
But the problem is that every time you create a new py file, you need to set this up once, which is still very troublesome.

Configuration item settings

  1. Use the config property of the Flask object to manipulate the value of the configuration.
    config is essentially a subclass of a dictionary, which can be manipulated like a dictionary:
    For example, the debug mode above isIt can be set in this form:
app = Flask(__name__)app.config['DEBUG'] = True

Or to update multiple configuration values ​​at once you can use the dict.update() method:

app.config.update(TESTING=True,SECRET_KEY=b'_5#y2L"F4Q8z\n\xec]/')
  1. When there are too many configuration items, you can create a configuration file and import these configuration information.
    We create a config.py file and write the corresponding key-value pair in it
    insert image description here
    then import in our main file, first import config
    then use app.config.from_object(config) will do.

In addition, app.config.from_pyfile() function, this method does not need to import config
directly app.config.from_pyfile('config.py') can be.

But I still haven't turned on the debug mode here, and JSON_AS_ASCII = False does take effect. I don't know why.

原网站

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