将request.user传递给Django中的模板 [英] Passing request.user to template in Django

查看:128
本文介绍了将request.user传递给Django中的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有另外一种方法来获取request.user不通过它从视图?我认为将request.user从视图中的所有函数传递到模板是非常错误的。是否有任何方法或方式,模板将获取数据库中的用户或任何对象?

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?

推荐答案

默认情况下(我是谈论Django版本1.3)你不需要改变TEMPLATE_CONTEXT_PROCESSORS。由于默认值已包含* django.contrib.auth.context_processors.auth *。

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*.

所以你的问题:默认情况下,你应该能够使用用户消息 perms 您的模板中的变量。例如:

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 %}

这篇关于将request.user传递给Django中的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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