Passing request.user to template in Django

蓝咒 提交于 2019-12-07 14:47:57

问题


Is there another way to get the request.user by not passing it from the views? I think passing request.user from all functions in views to the template is quite wrong. Is there any method or way that the template will get the user or any object in the database?


回答1:


By default (I am talking about Django version 1.3) you do not need change TEMPLATE_CONTEXT_PROCESSORS. Because default value already contains *django.contrib.auth.context_processors.auth*.

So to your question: By default, you should be able to use user, messages and perms variables in your template. For example:

User: {{user.username}}
{% if perms.appname.permname %}
  ... do something usefull ...
{% endif %}



回答2:


Context processors.



来源:https://stackoverflow.com/questions/7470539/passing-request-user-to-template-in-django

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