Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form sending error, Flask

There is form with two <input type="submit">. But when i'm sending it, second submit causes error.

layout:

<form action="{{ url_for('index') }}" method="post">     <input type="submit" name="add" value="Like">     <input type="submit" name="remove" value="Dislike"> </form> 

main.py:

... if request.method == 'POST':     if request.form['add']:         return redirect(url_for('index'))     elif request.form['remove']:         return redirect(url_for('index')) ... 

First submit(add) works well, but second(remove)...:

Bad Request The browser(or proxy) sent a request that this server could not understand.

How can i fix this error?

UPD:

It was pretty simple: request.form returns ImmutableMultiDict:

...  if 'Like' in request.form.values():      ... elif 'Dislike' in request.form.values():      ... 
like image 856
Dmitrijs Zubriks Avatar asked Dec 18 '11 15:12

Dmitrijs Zubriks


People also ask

How to handle error in Flask?

Step 1 — Using The Flask Debugger. In this step, you'll create an application that has a few errors and run it without debug mode to see how the application responds. Then you'll run it with debug mode on and use the debugger to troubleshoot application errors.

Why use WTF forms?

WTForms is a Python library that provides flexible web form rendering. You can use it to render text fields, text areas, password fields, radio buttons, and others. WTForms also provides powerful data validation using different validators, which validate that the data the user submits meets certain criteria you define.

What is WTF in Flask?

WTF stands for WT Forms which is intended to provide the interactive user interface for the user. The WTF is a built-in module of the flask which provides an alternative way of designing forms in the flask web applications.

How to raise exception in Flask?

Flask gives you the ability to raise any HTTP exception registered by Werkzeug. However, the default HTTP exceptions return simple exception pages. You might want to show custom error pages to the user when an error occurs. This can be done by registering error handlers.


1 Answers

As @Blubber points out, the issue is that Flask raises an HTTP error when it fails to find a key in the args and form dictionaries. What Flask assumes by default is that if you are asking for a particular key and it's not there then something got left out of the request and the entire request is invalid.

There are two other good ways to deal with your situation:

  1. Use request.form's .get method:

    if request.form.get('add', None) == "Like":     # Like happened elif request.form.get('remove', None) == "Dislike":     # Dislike happened 
  2. Use the same name attribute for both submit elements:

    <input type="submit" name="action" value="Like"> <input type="submit" name="action" value="Dislike">  # and in your code if request.form["action"] == "Like":     # etc. 
like image 81
Sean Vieira Avatar answered Oct 08 '22 18:10

Sean Vieira