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 Flask Basics Character Builder Cookies

Could anyone explain more about the "saves.get" used in the html templates?

Online documentations/references when we passed saves from app.py can be found here: http://flask.pocoo.org/docs/0.12/api/#flask.render_template

return render_template('builder.html', saves=get_saved_data(), options=DEFAULTS) 

But when used inside html templates, I can't find references or is there a way to know from python interpreter too?

Thanks in advanced!

1 Answer

Afdol Rizki
Afdol Rizki
23,158 Points

The saves variable is a dictionary that come from get_saved_data() method which convert the json object from cookie to a dictionary.

So, saves is a dictionary and a dictionary have a method called get, in this case saves.get() that returns a value of a specified key, the second parameter is the default value if the key is not found, saves.get('name', '').

The 'name' key come from the post request to '/save' route in request.form, because one of the input field in the form named 'name'. Hope its help you.

Thanks Afdol.