如何在 Django 中链接视图? [英] How to chain views in Django?

查看:21
本文介绍了如何在 Django 中链接视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施 James Bennett 出色的 django-contact-form 但遇到了障碍.我的联系页面不仅包含表单,还包含其他平面页面信息.

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.

我听说它提到这是可能的,但我似乎无法弄清楚如何使它起作用.我创建了自己的包装视图,称为联系表单视图,并尝试检查 HttpResponse 对象以获取我可以附加到的属性,但我似乎无法弄清楚是哪个(如果有).

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.

James 评论说可以找到最新的代码 此处 在 BitBucket.

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

推荐答案

  1. 编写一个使用 URL 查找适当平面页面对象的包装器.
  2. 从您的包装器中,调用(并从其中返回响应)联系表单视图,在 extra_context 参数中传递平面页面(除其他外,正是这种用途案例).
  3. 没有第三步.
  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.

这篇关于如何在 Django 中链接视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆