Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing request.user to template in Django

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?

like image 545
Joseph Lafuente Avatar asked Oct 21 '25 10:10

Joseph Lafuente


1 Answers

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 %}
like image 58
vasco Avatar answered Oct 24 '25 22:10

vasco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!