OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Flask not creating default session cookie. Why?

  • Thread starter Thread starter liathach7
  • Start date Start date
L

liathach7

Guest
I'm creating a basic web app in Flask. I want a way to identify users to persist between pages in the app, without the hassle of a user logging in. For this I'm making my app read the 'session' cookie that Flask by default sends to the browser, then storing the cookie value in a database.

Yesterday when I was tinkering around the session cookie was getting created each time I accessed the app. However today for some reason it is not, and I can't figure out why and what has changed between yesterday and today. Here is a sample of my code:

Code:
from app import app,db
from flask import render_template,url_for,request,make_response
from app.models import User
import sqlalchemy as sa

@app.route('/index')
def index():
    if request.cookies.get('session'):
        cookie_value=request.cookies.get('session')

        #   checks database to see if cookie already stored in database
        entries=db.session.query(User).filter(User.cookie_value==cookie_value)

        #deletes any present
        if e in entry:
           remove_entry(e)

        # add new cookie to database
        user=User(cookie_code=cookie_code)
        db.session.add(user)
        db.session.commit()
    else:
        print('cookie not found')
    
    return render_template('index.html')


@app.route('/page2')
def page2():
    #gets cookie
    if request.cookies.get('session'):
        cookie_value=request.cookies.get('session')
    else:
        print('cookie not found')

    #looks for it in database
    entry=db.session.query(User).filter(User.cookie_value==cookie_value)

    #does something with the cookie value matched in database
    for e in entry:
        orig_time=e.timestamp
    return render_template('page2.html',time=orig_time)

As I say, yesterday my flask app was creating a cookie named 'session' each time I accessed it through the browser. All I changed was basic updates to the app, not changing anything that I think would relate to how the app creates the cookie. And yet today it's just not there. I tried accessing through a different browser (edge) than my default(firefox) with the same result.

I need the session cookie to be created for my app to work. Does anyone have any insight into why it suddenly stopped creating the cookie? How to make it make the cookie again? Or perhaps I should set my own cookie rather than rely on it to make the session one? I'm happy to send extra code if it helps you understand my issue. Thanks for reading!
<p>I'm creating a basic web app in Flask. I want a way to identify users to persist between pages in the app, without the hassle of a user logging in. For this I'm making my app read the 'session' cookie that Flask by default sends to the browser, then storing the cookie value in a database.</p>
<p>Yesterday when I was tinkering around the session cookie was getting created each time I accessed the app. However today for some reason it is not, and I can't figure out why and what has changed between yesterday and today. Here is a sample of my code:</p>
<pre><code>from app import app,db
from flask import render_template,url_for,request,make_response
from app.models import User
import sqlalchemy as sa

@app.route('/index')
def index():
if request.cookies.get('session'):
cookie_value=request.cookies.get('session')

# checks database to see if cookie already stored in database
entries=db.session.query(User).filter(User.cookie_value==cookie_value)

#deletes any present
if e in entry:
remove_entry(e)

# add new cookie to database
user=User(cookie_code=cookie_code)
db.session.add(user)
db.session.commit()
else:
print('cookie not found')

return render_template('index.html')


@app.route('/page2')
def page2():
#gets cookie
if request.cookies.get('session'):
cookie_value=request.cookies.get('session')
else:
print('cookie not found')

#looks for it in database
entry=db.session.query(User).filter(User.cookie_value==cookie_value)

#does something with the cookie value matched in database
for e in entry:
orig_time=e.timestamp
return render_template('page2.html',time=orig_time)
</code></pre>
<p>As I say, yesterday my flask app was creating a cookie named 'session' each time I accessed it through the browser. All I changed was basic updates to the app, not changing anything that I think would relate to how the app creates the cookie. And yet today it's just not there. I tried accessing through a different browser (edge) than my default(firefox) with the same result.</p>
<p>I need the session cookie to be created for my app to work. Does anyone have any insight into why it suddenly stopped creating the cookie? How to make it make the cookie again? Or perhaps I should set my own cookie rather than rely on it to make the session one? I'm happy to send extra code if it helps you understand my issue. Thanks for reading!</p>
 

Latest posts

Top