views:

491

answers:

3

I'm implementing James Bennett's excellent django-contact-form but have hit a snag. My contact page not only contains the form, but also additional flat page information.

Without rewriting the existing view the contact form uses, I'd like to be able to wrap, or chain, the views. This way I could inject some additional information via the context so that both the form and the flat page data could be rendered within the same template.

I've heard it mentioned that this is possible, but I can't seem to figure out how to make it work. I've created my own wrapper view, called the contact form view, and attempted to inspect the HttpResponse object for an attribute I can append to, but I can't seem to figure out which, if any, it is.

EDIT: James commented that the latest code can new be found here at BitBucket.

+2  A: 

There's a context processor that may do what you want.

http://docs.djangoproject.com/en/dev/ref/templates/api/

You can probably add your various pieces of "flat page information" to the context.

S.Lott
The problem with that is I technically don't have source access to the view, so I can't change its response to use a different context. Such would be the case if the source were in a central repository and used by multiple projects, or perhaps in an .egg file.
Soviut
Context processors are not in your view function. They're separate from all view functions and are referenced in the settings.py file.
S.Lott
+1  A: 

Context processors are what you're thinking of. And render_to_response is irrelevant. The required piece of information is if the view uses RequestContext or not, as that is what activates context processors.

Other than those, there is no way to "chain" views to add to context - you can wrap one view in another and alter the data going into it, but you cannot add to context that way.

Magus
+1  A: 
  1. Write a wrapper which uses the URL to look up the appropriate flat page object.
  2. From your wrapper, call (and return the response from) the contact form view, passing the flat page in the extra_context argument (which is there for, among other things, precisely this sort of use case).
  3. There is no third step.
James Bennett
Thanks James, I think I need to apply the extra_context diff from your google code project to actually have the extra_context exposed in the contact_form view. You've got me wondering if I have the correct version of the code now, I think I'm using 0.3 ?
Soviut
Soviut is correct, SVN trunk of django-contact-form on Google code does not have the extra_context parameter, nor does v 0.3. I filed bug with patch here (http://code.google.com/p/django-contact-form/issues/detail?id=9), which is accepted but not yet applied.
Carl Meyer
Carl, check http://code.google.com/p/django-contact-form/issues/detail?id=9 for the diff to add the extra_content.
Soviut
Y'all want to head over here: http://bitbucket.org/ubernostrum/django-contact-form/overview/I've been slowly moving everything of mine to bitbucket.
James Bennett