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 trialAlan T.
4,784 PointsPurpose of saves=data?
At around 5:15 in the Cookies video, Kenneth passes in "saves=data" into the render_template for index. Can someone explain what the purpose of that was? Also, how is it being "read" in the code since index.html doesn't contain the word "saves" at all, it seems.
1 Answer
ygh5254e69hy5h545uj56592yh5j94595682hy95
7,934 Points# we use this function to retrieve saved data from cookies
def get_saved_data():
try:
# if there is cookie with name character, then we get the json version of it and save it to the variable data
data = json.loads(request.cookies.get('character'))
except TypeError:
data = {}
# here we just return the data variable
return data
@app.route('/')
def index():
# here we just create a data reference and we call the get_saved_data()
# so that we get back the json type that contains the name
data = get_saved_data()
# the saves= is the variable that we created there that will hold the data
# this allows us to call saves in the html file
return render_template('index.html', saves=data)
// because we pass the saves=data in the index(): return render_template we are now able to call the saves variable
// and retrieve the name key
value="{{ saves.get('name', '') }}"