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