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?
2 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 %}
-
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? Commented Sep 19, 2011 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.– vascoCommented Sep 19, 2011 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 Commented Sep 19, 2011 at 13:05
-
nevermind, got it already stackoverflow.com/questions/2246725/… Commented Sep 19, 2011 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.– vascoCommented Sep 19, 2011 at 13:18