Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top