py4web - using session object in a template

378 views
Skip to first unread message

Jim S

unread,
Jan 17, 2020, 12:16:38 PM1/17/20
to web2py-users
Hi

I'm trying to pass a session variable to layout.html but it tells me that session is not available:

NameError: name 'session' is not defined

I use this technique in web2py to send a page title to layout.html without having to pass it in the returned dict from the controller.

Obviously I'm being lazy using this technique.  Will this work with py4web or is it best for me to pass in explicitly in each controller?

-Jim

Jim Steil

unread,
Jan 17, 2020, 3:34:43 PM1/17/20
to web...@googlegroups.com
I should re-state my problem.  With web2py I wasn't using session, but response.  I added a couple of attributes that always got added to the response object.  Not sure how to mimic that in py4web, or would I even want to?

-Jim

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/cgg6M7B_wkI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/8848279b-e889-4b9b-90a6-664813a47b89%40googlegroups.com.

Massimo Di Pierro

unread,
Jan 21, 2020, 2:00:31 AM1/21/20
to web2py-users
I would recommend you do something like this

@action('index')
@action.uses('index.html', session)
def index():
    ...
    return dict(session=dict(session))

This will pass a shallow copy of the session to the view. You will be free to read from the session in index.html but you should not write it.



On Friday, 17 January 2020 12:34:43 UTC-8, Jim S wrote:
I should re-state my problem.  With web2py I wasn't using session, but response.  I added a couple of attributes that always got added to the response object.  Not sure how to mimic that in py4web, or would I even want to?

-Jim

On Fri, Jan 17, 2020 at 11:16 AM Jim S <ato....@gmail.com> wrote:
Hi

I'm trying to pass a session variable to layout.html but it tells me that session is not available:

NameError: name 'session' is not defined

I use this technique in web2py to send a page title to layout.html without having to pass it in the returned dict from the controller.

Obviously I'm being lazy using this technique.  Will this work with py4web or is it best for me to pass in explicitly in each controller?

-Jim

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/cgg6M7B_wkI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Jim Steil

unread,
Jan 21, 2020, 8:39:51 AM1/21/20
to web...@googlegroups.com
Thanks Massimo.  I was hoping to avoid having to pass something to the view every time.  I'm lazy, right?  I'm assuming that this is the same if I used response instead of session.  In web2py you didn't have to pass response to the view, it was automatically available.

-Jim

To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/cgg6M7B_wkI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/06f7bcfe-4a9f-42a0-a5ec-a686edf6513e%40googlegroups.com.
Message has been deleted

Massimo Di Pierro

unread,
Jan 22, 2020, 12:50:20 AM1/22/20
to web2py-users
Lazy is good. You can turn it to your advantage with this:

import functools
def my_awesome_decorator(path=None, method=['GET','POST'], requires_login=False):
    def wrapper(func, path=path, method=method, requires_login=requires_login):
        path = path or func.__name__
    template = func.__name__+'.html'
        fixtures = [template, db, session, auth]
        if requires_login: fixtures.append(auth.user)
        @action(path, method=method)
        @action.uses(*fixtures)
        @functools.wraps(func)
        def tmp(*a, **b):
            d = func(*a, **b)
            if isinstance(d, dict):
                 d['session'] = dict(session.local.data)
                 if requires_login: d['user'] = auth.get_user()
            return d
        return tmp
    return wrapper

@my_awesome_decorator('test')
def index():
    return {}
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/cgg6M7B_wkI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages