views:

268

answers:

2

Hi!

I want to include an initialized data structure in my request object, making it accessible in the context object from my templates. What I'm doing right now is passing it manually and tiresome within all my views:

render_to_response(...., ( {'menu': RequestContext(request)}))

The request object contains the key,value pair which is injected using a custom context processor. While this works, I had hoped there was a more generic way of passing selected parts of the request object to the template context. I've tried passing it by generic views, but as it turns out the request object isn't instantiated when parsing the urlpatterns list.

+1  A: 

To accomplish this, you will probably have to create your own middleware. That way, you have full control of the request, both before and after the view function.

Middleware is a very powerful concept, and not as hard to implement as it could seem, but don’t overdo it – it makes it hard to follow the program flow.

mikl
Yeah, that seemed to be the way to go. It correctly decorates my request object. We made a custom shortcut to replace 'render_to_response' - it simply uses 'render_to_response', but it passes along the RequestContext(request) argument, too!
Anders
A: 

I don't necessarily understand your question well enough.

Either you are complaining having to include the RequestContext in all views, in which case you need to write a wrapper that passes RequestContext for you. But you will still have to pass to it the request. If you don't want to pass that too, you may have to create your own middleware as mikl suggests.

Or, you are complaining about having to pass a lot of menu items, in each and every view. Which is wrong way to do it, you need to define a template context processor that ensures these are present in the template by default.

Lakshman Prasad