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
Mike Siwik
Mike Siwik
14,034 Points

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,842 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.

Ary de Oliveira
Ary de Oliveira
28,283 Points

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!