4

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 2

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 %}
5
  • 1
    ow, 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.
    – vasco
    Commented 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.
    – vasco
    Commented Sep 19, 2011 at 13:18
0

Context processors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.