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 Set a Cookie

Having trouble with response.set_cookie

Can't pass the third stage because I'm not clearly understanding how to set a cookie.

I had an issue with passing this question in the quiz too. In the video, the cookie is set with response.set_cookie('character', json.dumps(dict(request.form.items()))) but in the quiz, the solution was the simple response.set_cookie('name', "Treehouse")...

in that case, shouldn't my code below pass? What am I misunderstanding/ doing wrong?

flask_app.py
from flask import Flask
from flask import make_response

app = Flask(__name__)


@app.route('/save')
def save():
    response = make_response(redirect(url_for('index')))
    response.set_cookie('treehouse', "value")
    return response

This challenge must be bugged or something. to complete task 2 out of 3 all you need is:

   response = make_response(redirect(url_for('index')))

When it clearly asks you to return the response, but when you type in the return response, all you get are IndentationErrors.

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Ginny, your set_cookie is totally correct, but the problem is at the part two of the challenge, at this line of code

response = make_response(redirect(url_for('index')))

Part 2 is asking you to

Now add a variable named response and set it's value to make_response()

so you should simply just do this instead.

response = make_response()

That way your response will hold the correct value, and the response.set_cookie should work the way the challenge is expecting.

In the save() function, remove the pass statement. Now add a variable named response and set it's value to make_response(). You'll need to return the response variable.

response = make_response()

return response

Thanks, William! That fixed it!