session['views'] = views  is valid. This is because you are
setting(creating) a key, not getting it.

You can do...

some_id = 'my_id'
session[some_id] = 'some content'

and it will work just fine.

--
Thadeus





On Sun, May 9, 2010 at 8:06 AM, ionel <[email protected]> wrote:
> But anyway, I have an ASP.NET background. Session["key"] = value is
> normal.
> In PHP also: $_SESSION['views'] = $value;
>
> I see this as a limitation because I cannot persist an object into a
> session( if the session file it's not created) with a variable as key.
> For example:
>
> some_id = 'my_id'
> session[some_id] = 'some content'
>
> But I think, I need to learn the python way :)
>
> i
>
> On May 9, 8:47 am, ionel <[email protected]> wrote:
>> Thank you.
>> I didn't know.... :) I'm still a python noob, I think.
>>
>> On May 9, 2:03 am, Thadeus Burgess <[email protected]> wrote:
>>
>> > The reason is
>>
>> > session.button1 returns None if the key is not found.... whereas
>> > session['button1'] raises an exception when the key is not found.
>>
>> > This is typically python behavior.
>>
>> > --
>> > Thadeus
>>
>> > On Sat, May 8, 2010 at 11:44 PM, ionel <[email protected]> wrote:
>> > > I think I found a bug.
>>
>> > > Traceback (most recent call last):
>> > >  File "G:\dev\proj\python\web2py\gluon\restricted.py", line 178, in
>> > > restricted
>> > >    exec ccode in environment
>> > >  File "G:\dev\proj\python\web2py\applications\myapp/views\default/
>> > > index.html", line 97, in <module>
>> > >  KeyError: 'button1'
>>
>> > > If you don't have any session file in the session folder
>> > > session['button1'] it is not working, but session.button1 it does.
>> > > You need to delete all the session files to replicate the bug.
>>
>> > > Thanks.
>>
>> > > ionel
>>
>> > > On May 9, 12:05 am, ionel <[email protected]> wrote:
>> > >> Mea culpa!
>>
>> > >> Yes, they are equivalent. My key was empty... sorry. I think I'm
>> > >> tired...
>>
>> > >> Thank you very much.
>>
>> > >> ionel
>>
>> > >> On May 8, 11:27 pm, mdipierro <[email protected]> wrote:
>>
>> > >> > They should be equivalent. Can you show the traceback?
>>
>> > >> > On May 8, 9:54 pm, ionel <[email protected]> wrote:
>>
>> > >> > > But I allready did that... and its working...
>>
>> > >> > > Something like that:
>>
>> > >> > > if not session.c:
>> > >> > >     c = MyClass()
>> > >> > >     session.c = c
>> > >> > > else:
>> > >> > >     c = session.c
>>
>> > >> > > But my question was why I cannot use session['c'] instead of 
>> > >> > > session.c
>>
>> > >> > > Thank you!
>>
>> > >> > > i
>>
>> > >> > > On May 8, 9:31 pm, mdipierro <[email protected]> wrote:
>>
>> > >> > > > You cannot store your own classes in the session because the 
>> > >> > > > session
>> > >> > > > is retrieved before your own classes are defined. You can only 
>> > >> > > > store
>> > >> > > > in session primitive types. You can serialize your objects 
>> > >> > > > yourself.
>>
>> > >> > > > On May 8, 7:30 pm, ionel <[email protected]> wrote:
>>
>> > >> > > > > Hello,
>>
>> > >> > > > > I'd like to have something like that:
>>
>> > >> > > > > class MyClass():
>> > >> > > > >    def __init__(self, id)
>> > >> > > > >        self.id = id
>>
>> > >> > > > > c = MyClass('some_id')
>>
>> > >> > > > > session[c.id] = c
>>
>> > >> > > > > I do not see a solution for this.
>> > >> > > > > Can somebody help me?
>>
>> > >> > > > > Thanks.
>>
>> > >> > > > > i.
>

Reply via email to