librelist archives

« back to archive

Weird issue with session variables vanishing

Weird issue with session variables vanishing

From:
Mmn
Date:
2012-09-08 @ 15:41
Hello,

I have been using flask for my personal project for about 4 months now.
Today I am facing a strange issue with session variables. In one method I
am setting a session variable from the form values and sending back the url
for the target page in the response. When I load the target url, the
session variable is not existing. However another variable I have set
before is still there. I am completely at a loss on why this behaviour. Can
you please help?

I have put both my methods below.

@order_blueprint.route('/proceed/complete/<prefix>', methods=['POST'])
def proceed_to_complete(prefix):
    app.logger.debug('Create Entered %s'% request.form)
    user = session.get('user')
    app.logger.debug('Form %s'%request.form)
    session['form'] = request.form
    if user != None:
        session['family_id'] = request.form.get('family_id')
        session['address_id'] = request.form.get('address_id')
        session['pharmacist_id'] = request.form.get('pharmacist_id')
    else:
        #Cookie TBD
        session['pharmacist_id'] = request.form.get('pharmacist_id')
    # It prints correctly here.
    app.logger.debug(session.get('pharmacist_id'))
    ret = {}
    ret['status'] = "Success"
    ret['url'] = url_for('order_blueprint.complete', prefix=prefix)
    return json.dumps(ret)

@order_blueprint.route('/complete/<prefix>')
def complete(prefix):
    user = session.get('user')
    today = datetime.today()
    items = json.loads(session.get('cart'))
    pharmacist_id = session.get('pharmacist_id')
    # This value was set similarly and is fine.
    app.logger.debug('Cart in complete %s'%session.get('cart'))
    # All the below ones are None
    app.logger.debug(session.get('form'))
    app.logger.debug(session.get('family_id'))
    app.logger.debug(session.get('address_id'))
    app.logger.debug('Pharmacist id on complete %s'%pharmacist_id)
.....

Regards,

-- 
--
It’'s a fact, taller people sleep longer in bed.

Re: [flask] Weird issue with session variables vanishing

From:
Kerem Ulutaş
Date:
2012-09-08 @ 15:57
request.form is a proxy, see
http://flask.pocoo.org/docs/reqcontext/#notes-on-proxies

You should explicitly set every form key to the form value on the session,
otherwise you will lose them in the next request.

2012/9/8 MMN <mmn007@gmail.com>

> Hello,
>
> I have been using flask for my personal project for about 4 months now.
> Today I am facing a strange issue with session variables. In one method I
> am setting a session variable from the form values and sending back the url
> for the target page in the response. When I load the target url, the
> session variable is not existing. However another variable I have set
> before is still there. I am completely at a loss on why this behaviour. Can
> you please help?
>
> I have put both my methods below.
>
> @order_blueprint.route('/proceed/complete/<prefix>', methods=['POST'])
> def proceed_to_complete(prefix):
>     app.logger.debug('Create Entered %s'% request.form)
>     user = session.get('user')
>     app.logger.debug('Form %s'%request.form)
>     session['form'] = request.form
>     if user != None:
>         session['family_id'] = request.form.get('family_id')
>         session['address_id'] = request.form.get('address_id')
>         session['pharmacist_id'] = request.form.get('pharmacist_id')
>     else:
>         #Cookie TBD
>         session['pharmacist_id'] = request.form.get('pharmacist_id')
>     # It prints correctly here.
>     app.logger.debug(session.get('pharmacist_id'))
>     ret = {}
>     ret['status'] = "Success"
>     ret['url'] = url_for('order_blueprint.complete', prefix=prefix)
>     return json.dumps(ret)
>
> @order_blueprint.route('/complete/<prefix>')
> def complete(prefix):
>     user = session.get('user')
>     today = datetime.today()
>     items = json.loads(session.get('cart'))
>     pharmacist_id = session.get('pharmacist_id')
>     # This value was set similarly and is fine.
>     app.logger.debug('Cart in complete %s'%session.get('cart'))
>     # All the below ones are None
>     app.logger.debug(session.get('form'))
>     app.logger.debug(session.get('family_id'))
>     app.logger.debug(session.get('address_id'))
>     app.logger.debug('Pharmacist id on complete %s'%pharmacist_id)
> .....
>
> Regards,
>
> --
> --
> It’'s a fact, taller people sleep longer in bed.
>



-- 

Blog'umu okudunuz mu? http://www.ulutas.gen.tr

The box said "Requires Windows 95, NT, or better", so I installed Linux.

Re: [flask] Weird issue with session variables vanishing

From:
Mmn
Date:
2012-09-10 @ 02:54
Hello Kerem,

Thanks for the information, I wasn't aware of this (simply because I just
started working after just glancing through the docs. Mea Culpa).
However I was just printing the form to see if that was indeed the cause.
As you can see I am taking the value based on key and inserting into the
session. The behaviour is not very consistent either. Sometimes it works
continuously for 3-4 tests. Also yesterday I tried one more thing.

As I mentioned earlier the proceed_to_complete is called first and that
returns the url for complete to the UI. In this case I am putting
order_spec_id into session and I can see the value printed in the same
method. However on reaching the complete method it is None. From the
position where the logs are printed, proceed_to_complete is called before
complete. Could it be that the session value is not getting updated when
the complete method is called (it gets updated a little while later
perhaps) and that is causing this problem?
Please see the snippet below

@order_blueprint.route('/proceed/complete/<prefix>', methods=['POST'])
def proceed_to_complete(prefix):
    app.logger.debug('Create Entered %s'% request.form)
    <snip/>
            os = OrderSpecification()
            <snip/>
            db.session.add(os)
            db.session.flush()
            app.logger.debug('Order Spec Id %s'%os.id) #====> 5
            session['order_spec_id'] = os.id

@order_blueprint.route('/complete/<prefix>')
def complete(prefix):
           <snip/>
            app.logger.debug('Order Specification
%s'%session.get('order_spec_id'))  #====> None

Regards,
On Sat, Sep 8, 2012 at 9:27 PM, Kerem Ulutaş <1151986@gmail.com> wrote:

> request.form is a proxy, see
> http://flask.pocoo.org/docs/reqcontext/#notes-on-proxies
>
> You should explicitly set every form key to the form value on the session,
> otherwise you will lose them in the next request.
>
>
> 2012/9/8 MMN <mmn007@gmail.com>
>
>> Hello,
>>
>> I have been using flask for my personal project for about 4 months now.
>> Today I am facing a strange issue with session variables. In one method I
>> am setting a session variable from the form values and sending back the url
>> for the target page in the response. When I load the target url, the
>> session variable is not existing. However another variable I have set
>> before is still there. I am completely at a loss on why this behaviour. Can
>> you please help?
>>
>> I have put both my methods below.
>>
>> @order_blueprint.route('/proceed/complete/<prefix>', methods=['POST'])
>> def proceed_to_complete(prefix):
>>     app.logger.debug('Create Entered %s'% request.form)
>>     user = session.get('user')
>>     app.logger.debug('Form %s'%request.form)
>>     session['form'] = request.form
>>     if user != None:
>>         session['family_id'] = request.form.get('family_id')
>>         session['address_id'] = request.form.get('address_id')
>>         session['pharmacist_id'] = request.form.get('pharmacist_id')
>>     else:
>>         #Cookie TBD
>>         session['pharmacist_id'] = request.form.get('pharmacist_id')
>>     # It prints correctly here.
>>     app.logger.debug(session.get('pharmacist_id'))
>>     ret = {}
>>     ret['status'] = "Success"
>>     ret['url'] = url_for('order_blueprint.complete', prefix=prefix)
>>     return json.dumps(ret)
>>
>> @order_blueprint.route('/complete/<prefix>')
>> def complete(prefix):
>>     user = session.get('user')
>>     today = datetime.today()
>>     items = json.loads(session.get('cart'))
>>     pharmacist_id = session.get('pharmacist_id')
>>     # This value was set similarly and is fine.
>>     app.logger.debug('Cart in complete %s'%session.get('cart'))
>>     # All the below ones are None
>>     app.logger.debug(session.get('form'))
>>     app.logger.debug(session.get('family_id'))
>>     app.logger.debug(session.get('address_id'))
>>     app.logger.debug('Pharmacist id on complete %s'%pharmacist_id)
>> .....
>>
>> Regards,
>>
>> --
>> --
>> It’'s a fact, taller people sleep longer in bed.
>>
>
>
>
> --
>
> Blog'umu okudunuz mu? http://www.ulutas.gen.tr
>
> The box said "Requires Windows 95, NT, or better", so I installed Linux.
>



-- 
--
It’'s a fact, taller people sleep longer in bed.

Re: [flask] Weird issue with session variables vanishing

From:
Pitmairen
Date:
2012-09-15 @ 10:43
request.form is not a proxy, its just a MultiDict. It should be no problem
storing this in the session.

I think the problem is that you are maybe storing too much data in you
session. The session is stored in a cookie so you are limited to about 4KB
of data. Check the size of the cookie you are sending to see if this could
be the problem.


On Mon, Sep 10, 2012 at 4:54 AM, MMN <mmn007@gmail.com> wrote:

> Hello Kerem,
>
> Thanks for the information, I wasn't aware of this (simply because I just
> started working after just glancing through the docs. Mea Culpa).
> However I was just printing the form to see if that was indeed the cause.
> As you can see I am taking the value based on key and inserting into the
> session. The behaviour is not very consistent either. Sometimes it works
> continuously for 3-4 tests. Also yesterday I tried one more thing.
>
> As I mentioned earlier the proceed_to_complete is called first and that
> returns the url for complete to the UI. In this case I am putting
> order_spec_id into session and I can see the value printed in the same
> method. However on reaching the complete method it is None. From the
> position where the logs are printed, proceed_to_complete is called before
> complete. Could it be that the session value is not getting updated when
> the complete method is called (it gets updated a little while later
> perhaps) and that is causing this problem?
> Please see the snippet below
>
>
> @order_blueprint.route('/proceed/complete/<prefix>', methods=['POST'])
> def proceed_to_complete(prefix):
>     app.logger.debug('Create Entered %s'% request.form)
>     <snip/>
>             os = OrderSpecification()
>             <snip/>
>             db.session.add(os)
>             db.session.flush()
>             app.logger.debug('Order Spec Id %s'%os.id) #====> 5
>             session['order_spec_id'] = os.id
>
> @order_blueprint.route('/complete/<prefix>')
> def complete(prefix):
>            <snip/>
>             app.logger.debug('Order Specification
> %s'%session.get('order_spec_id'))  #====> None
>
> Regards,
>
> On Sat, Sep 8, 2012 at 9:27 PM, Kerem Ulutaş <1151986@gmail.com> wrote:
>
>> request.form is a proxy, see
>> http://flask.pocoo.org/docs/reqcontext/#notes-on-proxies
>>
>> You should explicitly set every form key to the form value on the
>> session, otherwise you will lose them in the next request.
>>
>>
>> 2012/9/8 MMN <mmn007@gmail.com>
>>
>>> Hello,
>>>
>>> I have been using flask for my personal project for about 4 months now.
>>> Today I am facing a strange issue with session variables. In one method I
>>> am setting a session variable from the form values and sending back the url
>>> for the target page in the response. When I load the target url, the
>>> session variable is not existing. However another variable I have set
>>> before is still there. I am completely at a loss on why this behaviour. Can
>>> you please help?
>>>
>>> I have put both my methods below.
>>>
>>> @order_blueprint.route('/proceed/complete/<prefix>', methods=['POST'])
>>> def proceed_to_complete(prefix):
>>>     app.logger.debug('Create Entered %s'% request.form)
>>>     user = session.get('user')
>>>     app.logger.debug('Form %s'%request.form)
>>>     session['form'] = request.form
>>>     if user != None:
>>>         session['family_id'] = request.form.get('family_id')
>>>         session['address_id'] = request.form.get('address_id')
>>>         session['pharmacist_id'] = request.form.get('pharmacist_id')
>>>     else:
>>>         #Cookie TBD
>>>         session['pharmacist_id'] = request.form.get('pharmacist_id')
>>>     # It prints correctly here.
>>>     app.logger.debug(session.get('pharmacist_id'))
>>>     ret = {}
>>>     ret['status'] = "Success"
>>>     ret['url'] = url_for('order_blueprint.complete', prefix=prefix)
>>>     return json.dumps(ret)
>>>
>>> @order_blueprint.route('/complete/<prefix>')
>>> def complete(prefix):
>>>     user = session.get('user')
>>>     today = datetime.today()
>>>     items = json.loads(session.get('cart'))
>>>     pharmacist_id = session.get('pharmacist_id')
>>>     # This value was set similarly and is fine.
>>>     app.logger.debug('Cart in complete %s'%session.get('cart'))
>>>     # All the below ones are None
>>>     app.logger.debug(session.get('form'))
>>>     app.logger.debug(session.get('family_id'))
>>>     app.logger.debug(session.get('address_id'))
>>>     app.logger.debug('Pharmacist id on complete %s'%pharmacist_id)
>>> .....
>>>
>>> Regards,
>>>
>>> --
>>> --
>>> It’'s a fact, taller people sleep longer in bed.
>>>
>>
>>
>>
>> --
>>
>> Blog'umu okudunuz mu? http://www.ulutas.gen.tr
>>
>> The box said "Requires Windows 95, NT, or better", so I installed Linux.
>>
>
>
>
> --
> --
> It’'s a fact, taller people sleep longer in bed.
>