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?
Asked
Active
Viewed 3,712 times
2 Answers
9
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 %}

vasco
- 1,502
- 1
- 16
- 19
-
1ow, i see. sorry I'm a noob. How bout an object in the database? Is there a way that the template can get it? – Joseph Lafuente Sep 19 '11 at 12:53
-
I'm not sure what do you mean by _object in the database_. If you have model (class in your model.py) and want to display it, you can use [generic views](https://docs.djangoproject.com/en/1.3/ref/generic-views/#list-detail-generic-views). – vasco Sep 19 '11 at 13:00
-
I have this model called SchoolSetting and It always contains 1 or more objects. I want to get the first object (SchoolSetting.objects.all()[0]) and put it in the default values of template_context_processors – Joseph Lafuente Sep 19 '11 at 13:05
-
nevermind, got it already http://stackoverflow.com/questions/2246725/django-template-context-processors – Joseph Lafuente Sep 19 '11 at 13:16
-
If you want to get only one object from database, you should use SchoolSetting.objects.get(pk=X), where X is ID of desired object. – vasco Sep 19 '11 at 13:18