何时使用上下文处理器 [英] When to use context processor
本文介绍了何时使用上下文处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{{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屋!
查看全文