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.
    

Leave a Comment