How do I access session data in Jinja2 templates (Bottle framework on app engine)?

2024/10/12 10:30:37

I'm running the micro framework Bottle on Google App Engine. I'm using Jinja2 for my templates. And I'm using Beaker to handle the sessions. I'm still a pretty big Python newbie and am pretty stoked I got this far :) My question is how do I access the session data within the templates? I can get the session data no problem within the actual python code. And I could pass the session data each time I call a jinja template. But since I need the session data in the main menu bar of the site... that means I would have to pass it on every single page. Does anyone know if I can access it directly in the templates?

For example I need the session data for my header links:

Home | FAQ | Login

or

Home | FAQ | Logout

Any help is greatly appreciated! :D

Answer

You can add things to the Jinja2 environment globals if you want them to be accessible to all templates. See this page for additional information.

Update:

A simple example is, for your setup code:

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))

Then, in your request handling code:

env.globals['session'] = session # Your session
# Your template can contain things like {{ session['key'] }}
template = env.get_template('mytemplate.html')
print template.render(the='variables', go='here')
#return response using rendered data
https://en.xdnf.cn/q/69662.html

Related Q&A

What is a dimensional range of [-1,0] in Pytorch?

So Im struggling to understand some terminology about collections in Pytorch. I keep running into the same kinds of errors about the range of my tensors being incorrect, and when I try to Google for a …

Pyinstaller executable keeps opening

Background I am working on face recognition following this link and I would like to build a standalone application using Python. My main.py script looks like the following. # main.py# Import packages a…

Occasional deadlock in multiprocessing.Pool

I have N independent tasks that are executed in a multiprocessing.Pool of size os.cpu_count() (8 in my case), with maxtasksperchild=1 (i.e. a fresh worker process is created for each new task). The mai…

How to kill a subprocess called using subprocess.call in python? [duplicate]

This question already has answers here:How to terminate a python subprocess launched with shell=True(11 answers)Closed 7 years ago.I am calling a command using subprocess.call(cmd, shell=True)The comma…

Printing one color using imshow [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Send headers along in python [duplicate]

This question already has answers here:Changing user agent on urllib2.urlopen(9 answers)Closed 9 years ago.I have the following python script and I would like to send "fake" header informatio…

How to download image to memory using python requests?

Like here How to download image using requests , but to memory, using http://docs.python-requests.org.

Accessing NumPy record array columns in Cython

Im a relatively experienced Python programmer, but havent written any C in a very long time and am attempting to understand Cython. Im trying to write a Cython function that will operate on a column o…

scipy append all rows of one sparse matrix to another

I have a numpy matrix and want to append another matrix to that.The two matrices have the shapes:m1.shape = (2777, 5902) m2.shape = (695, 5902)I want to append m2 to m1 so that the new matrix is of sh…

Add argparse arguments from external modules

Im trying to write a Python program that could be extended by third parties. The program will be run from the command line with whatever arguments are supplied.In order to allow third parties to creat…