How to obtain Dict from a Flask Request Form

·

0 min read

The request.form returns an immutable dictionary object [ImmutableMultiDict]. Which means its a frozen object and that you cannot change the values. However in some cases you might wanna change the values. Such as creating a slug from a title before saving to a database. In such case, you need a plain old dictionary object.

@app.route("/path", methods=["POST"])
def handle_post_request():
    if request.method == "POST":
        data = request.form.to_dict()
        data['some_key'] = "Some Value"
        # ... do something with data ... 
    return redirect("/")

You just call to_dict() on the request.form object and you get a dictionary you can work with.