• Now all data be ready for publish, We had made our blog alive now. Haha.



  • These days CCP GFW blocked all the IPs from US and when I switch my IP in google Cloud, the disk data cannot be restore again. I have to restart my blog totally, Now the website is still building…



Form sending error, Flask- Stack Overflow

Python PingBook 1 months ago (10-23) 19 0

Question

There is form with two . But when i’m sending it, second submit causes error.

layout:



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():

Answer

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:

Use request.form’s .get method:

if request.form.get(‘add’, None) == “Like”:
# Like happened
elif request.form.get(‘remove’, None) == “Dislike”:
# Dislike happened

Use the same name attribute for both submit elements:


# and in your code
if request.form[“action”] == “Like”:
# etc.




Copyright from PingBook Blog, If not specified, they are original. This site uses BY-NC-SAProtocol authenticated.
For reprinting, please indicate the link of the original text:Form sending error, Flask- Stack Overflow
LIKE (0)
[1725641479@qq.com]
SHARE (0)
PingBook
Author:
We create, We sharing! Tag every value data your sharing
Submit comments
Cancel comments
emoji picture bold strikethrough center italic check in

Hi,you need to provide your name and email adress!

  • Name (Required)
  • Email (Required)
  • Website