Form Sending Error, Flask

Form sending error, Flask

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.

What is the cause of the Bad Request Error when submitting form in Flask application?

The solution was simple and uncovered in the comments. As addressed in this question, Form sending error, Flask, and pointed out by Sean Vieira,

...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.

In other words, if only one form element that you request in Python cannot be found in HTML, then the POST request is not valid and the error appears, in my case without any irregularities in the traceback. For me, it was a lack of consistency with spelling: in the HTML, I labeled various form inputs

<input name="question1_field" placeholder="question one">

while in Python, when there was a POST called, I grab a nonexistent form with

request.form['question1']

whereas, to be consistent with my HTML form names, it needed to be

request.form['question1_field']

Flask Error Not Found URL On server custom Form System

So i found an answer. So flask doesnt allow specifc links such as https://github.com/codingdudepy to be returned as a redirect url. Nor does it allow things like #, or domains like .com for some reason. When i removed these things it worked perfectly.

Flask form submit both succeeds and errors

I looked into it because of the @NoCommandLine answer. The point is, that the all_items function is located in the blueprint, not in the base of the application. To redirect to it you want to write redirect(url_for(".all_items") (notice the full stop at the first position of the string).See the documentation for url_for, there is an example for a blueprint containing an index function. The full stop makes it search in the same blueprint the current route is in.

Python Flask validate_on_submit() https error

I'm pretty sure that Mandraenke's comment is right. Now, your route allows only GET method, by form.validate_on_submit() presumes POST method. So you need to specify it explicitly. Like that:

@app.route("/category/add", methods=['GET', 'POST'])
def add_category():
form = AddCategory()
if form.validate_on_submit():
new_category = Categories(
title=form.name.data,
description=form.description.data,
review=form.review.data,
img_url=form.img_url.data,
)
db.session.add(new_category)
db.session.commit()
return render_template("index.html")
return render_template("add_category.html", form=form)

Submitting a HTML form in Flask creates error with request.form

Your answer inputs need to be inside the form tag to be submitted - there is no 'answer' element in the form you are submitting



Related Topics



Leave a reply



Submit