VocabReminder
Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

How To Get A Dict from Flask Request Form

       

The request.form object is an immutable dictionary. An ImmutableMultiDict object to be exact. Immutable means that you cannot change its values. It's frozen.

However, you might want to change the values. Say for instance, creating a slug from a title before saving to a database. In such a case, what you need is a plain old dictionary object.

Here is what that might look like.

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

Tagged w/ #python #flask #web development