Multiple Template.Render?

60 views
Skip to first unread message

b00134n

unread,
Jun 11, 2008, 9:55:39 PM6/11/08
to Google App Engine
I am new to Python/Django, so please forgive me if this is a stupid
question...

Is there any way to have multiple Template.Render calls to render
multiple dictionaries to the template (I think they're called
dictionaries)?

For instance, I currently have a login function included in a base.py
file that checks if the user is logged in and renders the appropriate
login/logout text and links back to the template.

My login function in base.py looks like this:

class login(webapp.RequestHandler):
def get(self):
current_page = self.request.path.split('/')[-1]
if not current_page:
current_page = 'index.html'

... stuff ...

path = os.path.join(os.path.dirname(__file__), current_page)
self.response.out.write(template.render(path, template_values))

My main.py file calls base.login successfully in the following way:

def main():
application = webapp.WSGIApplication(
[(r'/.*\.html', base.login),
('/', base.login)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

Now the problem is, I would like to be able to call base.login in an
alternative way like this...

class get_content(webapp.RequestHandler):
def get(self):
base.login()

... stuff ...

path = os.path.join(os.path.dirname(__file__), current_page)
self.response.out.write(template.render(path, template_values))

def main():
application = webapp.WSGIApplication(
[('/browse.html',
get_content)],
debug=True)

wsgiref.handlers.CGIHandler().run(application)

When I try to do this it renders a black page. I think I understand
why, but do not see any way to accomplish what I would like to do.

Is there a better way to go about passing values back to the template?

Any help would be appreciated.

Thanks!

Daniel O'Brien

unread,
Jun 12, 2008, 2:42:44 PM6/12/08
to Google App Engine
Template.render is a fairly simple method, taking in a single
dictionary and rendering the template to a string based on that data.
If you're interested in rendering multiple dictionaries, you can
simply merge the existing contents. e.g.

template_values = {'foo': 1}
new_template_values = {'bar': 2}

template_values.update(new_template_values)
# template_values should now be {'foo': 1, 'bar': 2}

path = os.path.join(os.path.dirname(__file__), current_page)
self.response.out.write(template.render(path, template_values))

Does this address the use case you're dealing with?

Daniel

b00134n

unread,
Jun 12, 2008, 9:06:28 PM6/12/08
to Google App Engine
Thank you for responding, Daniel.

The solution you posted is helpful and similar to one I came up with
on my own yesterday.

Essentially, I removed the template rendering from login, altered the
class as shown below, and have it return the template values I need.

class login():
@staticmethod
def get(self):

From within other files I call login like this...

template_values = base.login().get(self)

After that I just append additional values to template_values as
needed and then do the rendering.
Reply all
Reply to author
Forward
0 new messages