简体   繁体   中英

Can you set session variables in MAKO files in Pyramid?

So I found this answer regarding setting session variables in a pyramid view file, and then later accessing it in a mako template. ( How to access session variable in Mako template and Pyramid? )

I wanted to know if you could do it the other way around. So instead of:

Pyramid view.py

def thisView(request):
    session = request.session
    session['selectedclientid'] = 'test' #selectedclient.id
    session.save()

webpage.mako

${request.session['selectedclientid']}

Can I swap it so I can do this instead?

webpage.mako

${request.session['selectedclientid'] = '5'}

Pyramid view.py

def thisView(request):
    someLogicOn(session['selectedclientid'])

So far I have been unsuccessful in making it work and I'm not sure if it's just due to a lack of understanding how to do it or if it's something that just can't be done. Any advice would be great!

In the typical rendering workflow, the view executes before the renderer. It's not clear how you are intending to correct for that. It's possible to do if you call render yourself within the view, I guess, so I'll show that.

webpage.mako:

<%
request.session['selectedClientId'] = '5'
%>

code:

def thisView(request):
    response = render_to_response('webpage.mako', {}, request=request)
    someLogicOn(request.session['selectedClientId'])
    return response

This is logically a little backward though, so you might want to think twice about what you're doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM