2

I'm hoping to use a context processor or middleware to modify the values of the dictionary passed to render_to_response prior to the actual rendering. I have a messaging schema I'm trying to implement that would populate a message list based on the existence of a type of user that I'd like to search the context for prior to the rendering of the template.

Example:

def myview(...):
    ...
    return render_to_response('template.html',
        {'variable': variable},
    )

and I'd like to be able to add additional information to the context on the existence of 'variable'.

How can I access 'variable' after my view defines it but before it gets to the template so that I can further modify the context?

garromark
  • 814
  • 8
  • 20
  • How can I access 'variable' after my view defines it but before it gets to the template so that I can further modify the context. – garromark Feb 12 '12 at 00:26

3 Answers3

5

use TemplateResponse:

from django.template.response import TemplateResponse

def myview(...):
    ...
    return TemplateResponse(request, 'template.html', 
        {'variable': variable},
    )

def my_view_wrapper(...):
    response = my_view(...)
    variable = response.context_data['variable']
    if variable == 'foo':
        response.context_data['variable_is_foo'] = True
    return response
Mikhail Korobov
  • 21,908
  • 8
  • 73
  • 65
  • This was the response to my question :) The function my_view_wrapper from the comment I implemented as a process_template_response() in my middleware, which then gains access to response.context_data, which has the data I required. Thanks! – garromark Feb 12 '12 at 02:37
2

This is easy. If you have supplied just a little bit more code in your example the answer might have bit you.

# first build your context, including all of the context_processors in your settings.py
context = RequestContext(request, <some dict values>)
# do something with your Context here
return render_to_response('template.html', context)

Update to comment:

The result of a render_to_response() is an HTTPResponse object containing a template rendered against a Context. That object does not (to my knowledge) have a context associated with it. I suppose you could save the result of render_to_response() in a variable and then access the Context you passed it, but I'm not sure what problem you are trying to solve.

Did you modify the Context during rendering? If so you may find that the information is not there any longer because the Context has a scope stack which is pushed/popped during template processing.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65
  • I don't see how my settings.py will know which user type is being accessed on page load. I want different information to be loaded based on what type of user is being accessed, however this is a universal action, so it shouldn't need to be done in each view. Is there a way to access the context/view-generated-dictionary AFTER the render_to_response() is called? – garromark Feb 12 '12 at 00:41
0

You can create a dictonary for the context:

def myview(...):
    c = dict()
    c["variable"] = value
    ...
     do some stuff
    ...
    return render_to_response('template.html',c)

Maybe the RequestContext is the thing you are looking for.

blacklwhite
  • 1,888
  • 1
  • 12
  • 17
  • In a middleware process_response I used RequestContext(response) and it was missing all of the view-defined variables, like c["variable"] in your response. Also, I want it automated outside of the view if possible, since I'll be loading these variables in multiple locations. Sorry if I haven't been 100% clear :) – garromark Feb 12 '12 at 00:37