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

Sasan Shadpour
Sasan Shadpour
2,601 Points

response.set_cookie is not working in VSCode with anaconda!

I am trying to duplicate what Kenneth does in the cookie section on VS Code.

my problem is that set_cookie as well as request.form.items are not identified by VS and I cannot get any cookie on the inspector web page.

the code I am having is

from flask import Flask
from flask import render_template
from flask import redirect
from flask import url_for
from flask import request
from flask import make_response
import json

app = Flask(__name__)

@app.route('/')

def index():
    return render_template('index.html')

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

app.run(debug=True) #host='0.0.0.0', port=8000

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

The code appears to be working-- that is, the cookie is being set in the browser. You may be trying to test or check the cookie too early in its lifecycle.

You are not going to see or be able to inspect the cookie until the request has been processed.

For example-- I am using a simple form with a field called "search" where the user can type in a search for a character by name. On the index page, I type in Santa Claus and then press the "search button". Then it POSTs to a second route called /save with a method called save(). The save method picks up the user request request.form.items() and converts it to JSON and saves it unencrypted in the cookie named character and then redirects to the /index route.

I definitely see the cookie is set to the correct value when I am back at the index route using the Chrome browser. Using the inspect -> application -> cookies feature of Chrome-- it is also similar in Edge or Firefox.

index.html

<form method="POST" action="/save">
    <input type="text" name="search" placeholder="Search">
    <input type="submit" value="Search">
</form>

If you are trying to use pdb, do so on the index route. You will see your cookie that was set in the previous request.

def index():
    import pdb; pdb.set_trace()
    return render_template('index.html')

In the console--

(Pdb) import flask
(Pdb) flask.request.cookies     
ImmutableMultiDict([('character', '{"search": "Santa Claus"}')])
(Pdb) continue
Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Good news. Your code is syntactically valid.

Are you using the correct Python anaconda interpreter that has Flask installed? You might be using the default Python.exe that you installed with VSC which doesn't have Flask pip installed.

Possible fix---

Hit F1 in your Visual Studio Code and then type, "Python: Select interpreter" then click on "+ Enter interpreter path..." then click on "Find..." and now browse for the folder with Anaconda and select python.exe.

Sasan Shadpour
Sasan Shadpour
2,601 Points

Thanks Jeff.

I am almost certain that anaconda works fine as I can work with flask with no problem.

The problem I am having is I cannot find the cookie on the inspector web page. And when I check it pdb or request I can see that the code works just fine (meaning that it shows the results I am asking for).