When to use context processor

本秂侑毒 提交于 2019-12-08 02:41:52

问题


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 ?


回答1:


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.




回答2:


using additional user data in your templates is easy:

{{ user.get_profile.foobar }} where foobar is one of your profile's fields. But make sure to have set AUTH_PROFILE_MODULE = 'yourprofileapp.YourProfileModel' in settings.py. For more info see http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users



来源:https://stackoverflow.com/questions/2880940/when-to-use-context-processor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!