Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to chain views in Django?

Tags:

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.

like image 488
Soviut Avatar asked Feb 03 '09 01:02

Soviut


2 Answers

  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.
like image 73
James Bennett Avatar answered Sep 30 '22 17:09

James Bennett


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.

like image 44
S.Lott Avatar answered Sep 30 '22 17:09

S.Lott