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

Ayman Said
seal-mask
.a{fill-rule:evenodd;}techdegree
Ayman Said
Python Web Development Techdegree Student 14,717 Points

Any reason why Save() view has been changed? enhancement?

during the video save() view has been changed from:

    @app.route('/submitted', methods=['POST'])
    def save():
        response = make_response(redirect(url_for('index')))
        response.set_cookie('character', json.dumps(dict(request.form.items())))
        return response

To:

@app.route('/submitted', methods=['POST'])
def save():
    response = make_response(redirect(url_for('index')))
    data = get_saved_data()
    data.update(dict(request.form.items()))
    response.set_cookie('character', json.dumps(data))
    return response

What is the enhancement here? as from general design perspective, seems better to update an existing data structure, but anyway we are making it by dict(request.form.items()) ?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

In the first code, the cookie is assigned based on the contents of the form.

But what if there are attributes in the cookie data that the form does not change or update. These would get cleared.

So, the second code retrieves all of the cookie information sent in the request, then updates this information with the new data from the form. Sort of a merge of old and new data. Finally, the merged data is returned in the response so all the cookies remain intact.

Post back if you need more help. Good luck!!