Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask Basics Character Builder Cookies

Alan T.
Alan T.
4,784 Points

Purpose 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

7,902 Points
# we use this function to retrieve saved data from cookies
def get_saved_data():
        # 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

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', '') }}"