使用 Google App Engines webapp 框架,有什么方法可以将数据传递给基本模板吗?
具体来说,我只想在用户登录时显示一个注销按钮(使用谷歌自己的身份验证系统)。
我还在学习,所以我不确定哪些部分是特定于 GAE 的,哪些部分是特定于 Django 的;必须从每个请求处理程序发送登录用户似乎非常不干。
最佳答案
基本模板的参数与任何其他模板参数的传递方式相同,即传递给 template.render。我通常通过在我的基本处理程序中插入通用模板参数的便捷方法来解决这个问题,如下所示:
class BaseHandler(webapp.RequestHandler):
def render_template(self, filename, template_args):
path = os.path.join(os.path.dirname(__file__), 'templates', filename)
template_args.update({
'user': users.get_current_user(),
# ...
})
class MyHandler(BaseHandler):
def get(self):
self.render_template('my.html', {'foo': 'bar'})
关于google-app-engine - 如何将数据传递给 GAE 基本模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6765397/