SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

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

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app