Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

have an object available in every templates

31 views
Skip to first unread message

Bastian

unread,
Jun 28, 2012, 8:11:37 AM6/28/12
to django...@googlegroups.com
Hi,

I don't know if this is possible or how to do it. I would like to write a sort of context processor where I pass an object to the template. I have always passed strings to these variables. Let's say I have a Book model and in a view I list the latest books I've added to the database. In the corresponding template I will then have an objects list with these objects. But I also want to be able to call a variable containing another list of objects, say my favorite books, without having to pass it explicitly from the view and being able to call it from any template.
Can I do it with context processors, or anything else actually?

Thanks.

Karl Sutt

unread,
Jun 28, 2012, 9:20:58 AM6/28/12
to django...@googlegroups.com
I think you can do it with Middleware. https://docs.djangoproject.com/en/1.4/topics/http/middleware/#writing-your-own-middleware
Specifically, I think it's worth looking into the "process_template_response" method.

Tervitades/Regards
Karl Sutt



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/sLXf508kb4gJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Daniel Roseman

unread,
Jun 28, 2012, 9:24:21 AM6/28/12
to django...@googlegroups.com
Use a custom template tag.
--
DR. 
Message has been deleted

Bastian

unread,
Jun 28, 2012, 11:53:46 AM6/28/12
to django...@googlegroups.com
Well, I was curious to see if it worked with a context processor so I tried and indeed it works. After all it's only returning an object or list of objects in a dictionary. Now I don't know which method is the most efficient.

bruno desthuilliers

unread,
Jun 29, 2012, 6:51:09 AM6/29/12
to django...@googlegroups.com


On Thursday, June 28, 2012 5:53:46 PM UTC+2, Bastian wrote:
Well, I was curious to see if it worked with a context processor so I tried and indeed it works. After all it's only returning an object or list of objects in a dictionary. Now I don't know which method is the most efficient.


The context_processor will be called for each and every RequestContext (so for each and every view AND templatetag creating a RequestContext), so better to avoid anything "intensive" here.

Also, it won't have access to the existing context - which may or not be a problem, but for something "my favorite books" (which really means : "the favorité books of request.user"), you can bet one day or another you will want to show "the favorite books of _another_ user", and this is something that won't work with a context processor.

To make a long story short : use a custom template tag that will take the user as an argument.

My 2 cents...

 

Bastian

unread,
Jun 29, 2012, 12:50:16 PM6/29/12
to django...@googlegroups.com
Good point, thanks for your input.
Reply all
Reply to author
Forward
0 new messages