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 Starting the Builder

How did we set the cookie for colors? [solved?]

Hi,

I can't grasp how did we set the cookie for the color in this video, we explicitly set the cookie in the last video with

response.set_cookie('character_name', json.dumps(data))

but in here it is not explained how we did it.

Your answer will be very appreciated!

1 Answer

Ok here's the result of my investigation, please correct me if I am wrong. We will not set a new cookie but just add a key-value pair to the existing cookie.

  1. the user chooses the color, and hits update. The form element will then send a POST request to the /save view
  2. the /save view will then redirect us back to the /builder view
  3. /save will call the get_saved_data() method and then will update the cookie (data)
  4. the new data will be stored in request.form which will now have an immutablemultidict with an extra key 'colors' names in the input element and it's value '#chosen color'
  5. we will have the color available on the builder.html because saves.get('color') will have the 'checked' attribute attached to it which tells the CSS doc which background color to display and which radio button to be selected