Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python flask not creating cookie when setting expiration

I have this python-flask project and I require logic that will create a cookie if the cookie does not exist.

Once the cookie is created, I need to store the cookie, among other values, into a database for tracking purposes.

Here is a partial of my current code:

import uuid
from flask import render_template, request, make_response

def load_cookie_config(context):
    #context is a dictionary that is passed

    key = 'some_key'
    template_name = 'some_template'
    cookie = request.cookies.get(key, None)

    if not cookie:
        guid = str(uuid.uuid4())
        context['cookie_quid'] = guid

        rsp = make_response(render_template(template_name, **context))

        rsp.set_cookie(key, guid)

        #... some method call insert values into db

    else:
       result = '' #.. some method call to get values from db
       context['cookie_quid'] = cookie
       rsp = make_response(render_template(template_name, **context))

    return rsp

The code above works and it creates the cookie as expected, however it defaults the cookie to expire whenever the browser session ends.

I need to set the cookie to where it will expire after an X amount of days vs. browser session, but when I change this line:

rsp.set_cookie(key, guid)

to

rsp.set_cookie(key, guid, expires=90)

It does not create the cookie at all.

Does anybody have thoughts or ideas on to why this might be?

like image 951
Wondercricket Avatar asked Oct 28 '14 16:10

Wondercricket


People also ask

How do you set the expiry period for a cookie object?

Just set the expires parameter to a past date: document. cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; You should define the cookie path to ensure that you delete the right cookie.

How do you set cookies in a python Flask?

Flask cookies In Flask, set the cookie on the response object. Use the make_response() function to get the response object from the return value of the view function. After that, the cookie is stored using the set_cookie() function of the response object. It is easy to read back cookies.

How do I set cookie in response header Flask?

In Flask, cookies are set on response object. Use make_response() function to get response object from return value of a view function. After that, use the set_cookie() function of response object to store a cookie. Reading back a cookie is easy.

Which type of cookies is used to assign an expiration date?

Persistent cookie Persistent cookies are not deleted by the browser when the user closes it. These cookies have an expiration date that you can set in your server.


1 Answers

You need to set the max_age=90 * 60 * 60 * 24, or use a datetime for your expires:

Example:

import datetime
expire_date = datetime.datetime.now()
expire_date = expire_date + datetime.timedelta(days=90)
response.set_cookie(key, guid, expires=expire_date)
like image 162
CodeLikeBeaker Avatar answered Sep 28 '22 04:09

CodeLikeBeaker