何时使用上下文处理器 [英] When to use context processor

查看:178
本文介绍了何时使用上下文处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中包含一个userBox,其中显示了每个记录的用户(您的姓名,头像等)的一些数据。从我已经知道的django看起来,显而易见的是,我应该添加用户到上下文处理器的查询(所以我可以使用这个包含的userBox中的 {{user}} 变量)。但是在使用django-lfs商店时,我注意到,它的模板正在使用 {{user}} 变量,这个变量没有添加到上下文处理器或模板标签。有没有其他方式可以在我的模板中获取这些用户?

解决方案

它是由

  django.contrib.auth.context_processors.auth 

问题的答案是:如果您需要在每个模板中使用某些内容,则应使用context_processor。


I have a site on which I'm including a userBox with some data shown for each logged user (your name, avatar etc). From what I already know about django it seems obvious, that I should add query for user to context processor (so that I can use{{user}} variable in this included userBox ). But while using django-lfs shop I've noticed, that it's templates are using {{ user }} variable which is nowhere added to context processors nor template tags. Is there any other way to obtain user in my template than those 2 ?

解决方案

It's added by

django.contrib.auth.context_processors.auth

And the answer for the question is: if you need something to be in every template, you should use context_processor.

这篇关于何时使用上下文处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆