Ok, I understand. I create a session variable with

session.my_var = "some content"

but not with session['some_id'] = "some content" (web2py will raise an
exception if the session variable does not exists), and I get the
content with

another_var = session.my_var
or
another_var = session['my_var']

Anyway, my_var is a hard coded key in this situation. I cannot create
dynamically session variables. I think this is not a good practice.

All other frameworks and django also do this.

http://www.djangobook.com/en/2.0/chapter14/
------------------------------------------------------
# Set a session value:
request.session["fav_color"] = "blue"
------------------------------------------------------

ionel



On May 9, 12:49 pm, Thadeus Burgess <thade...@thadeusb.com> wrote:
> 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 <ionelanton...@gmail.com> 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 <ionelanton...@gmail.com> wrote:
> >> Thank you.
> >> I didn't know.... :) I'm still a python noob, I think.
>
> >> On May 9, 2:03 am, Thadeus Burgess <thade...@thadeusb.com> 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 <ionelanton...@gmail.com> 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 <ionelanton...@gmail.com> 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 <mdipie...@cs.depaul.edu> wrote:
>
> >> > >> > They should be equivalent. Can you show the traceback?
>
> >> > >> > On May 8, 9:54 pm, ionel <ionelanton...@gmail.com> 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 <mdipie...@cs.depaul.edu> 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 <ionelanton...@gmail.com> 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