Re: when is a Context Processor called?
On Mon, Oct 8, 2012 at 3:28 PM, Stefano T
<stefano.tranquillini@gmail.com> wrote:
> Ok.
> so basically they are called before the rendering of a template.
>
> How can i have an object stored in the request object (if possible)?
>
> Something that lets me to do, in a view: request.user_profile...
>
You can define a simple middleware to put attributes on a request. Eg:
class MyMiddleware(object):
def process_request(request):
request.profile = None
if request.user.is_authenticated():
request.profile = request.user.get_profile()
See the documentation on middleware:
https://docs.djangoproject.com/en/1.4/topics/http/middleware/
Note that anything you put in TEMPLATE_CONTEXT_PROCESSORS is run
whenever you render a template, and anything in MIDDLEWARE_CLASSES
will be run on each request, so make sure the things you do in those
places are actually required every time you render a template /
process a request.
Cheers
Tom
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home