Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

request.form.items() returns the the same result as request.form !!

First, I would like to thank Kenneth Love and the team at Treehouse for such great courses.

As far as I know, request.form returns an ImmutableMultiDict object, which can be converted into a dictionary dict(request.form) because an ImmutableMultiDict object is like a dict_items().

In the "Cookies" video, Kenneth used dict(request.form.items()). I found that dict(request.form) will do the trick.

This is because ImmutableMultiDict allows for a single key to have more than one value. So, if we have two form fields with the same value then dict(request.form.items()) will return the first value of that key. However, dict(request.form) will return a "list" of all the values of that same key.

Example:

my_object = ImmutableMultiDict ([('a', 1), ('a', 2), ('b', 3)])
dict(my_object.items())

The result is: {'a': 1, 'b': 3}

While this code:

dict(my_object)

returns: {'a': [1, 2], 'b': 3}

Therefore, the "cute bear" template should be returned as follows:

return render_template("index.html", saves=" ".join(data["name"]))

Please correct me if I am wrong!

Thanks