exception for non existing parameter in FLASK

后端 未结 3 1373
梦谈多话
梦谈多话 2021-01-02 06:57

I have a form that sends parameters. In my form I have a checkbox. If my checkbox is not checked then I will not get any parameters.

If in my module I have :

相关标签:
3条回答
  • 2021-01-02 07:10
    var = request.form.get('checkbox')
    

    This will return None if the parameter is not defined.

    0 讨论(0)
  • 2021-01-02 07:16

    The reason for this is hidden in the flask documentation here: http://flask.pocoo.org/docs/api/#flask.Flask.trap_http_exception

    I'm not sure why this behaviour isn't changed for debugging mode, but to get Flask to catch the BadRequestKeyError exceptions from werkzeug, you need to run something like this on your application object:

    app.config['TRAP_BAD_REQUEST_ERRORS'] = True
    
    0 讨论(0)
  • 2021-01-02 07:32

    I had similar issue during validation of single boolean field.

    I solved it by using remember = form.rememberme.data instead of request.form['remember_me']

    0 讨论(0)
提交回复
热议问题