在 Django 中滚动你自己的通用视图

发布于 2024-11-17 23:11:59 字数 781 浏览 3 评论 0原文

Django 文档在基于类的通用视图中提到 DetailView 由以下部分组成:View、SingleObjectMixin 和 SingleObjectTemplateResponseMixin。我正在尝试这个,因为我有兴趣创建一个通用视图,该视图将使用 ModelForm 执行 object_detail 视图,以便可以自动生成我的模型行。

为了尝试复制 DetailView,我尝试创建一个类,如下所示:

from django.views.generic import list_detail, View
from django.views.generic.detail import (SingleObjectMixin,
    SingleObjectTemplateResponseMixin, BaseDetailView)

class formdisplay(View,SingleObjectMixin,SingleObjectTemplateResponseMixin): pass

当我使用 formdisplay 而不是 list_detail.object_detail 时,我收到错误

TypeError at /inpatient-detail/4/
__init__() takes exactly 1 non-keyword argument (2 given)

有关如何执行此操作的任何提示吗?

另外,关于如何编写导入语句的文档在哪里?我必须通过谷歌来查找要导入的内容,因为我在文档中找不到该内容。

提前致谢, 史蒂夫

The Django documentation mentions in the Class-based generic views that the DetailView is composed from: View, SingleObjectMixin, and SingleObjectTemplateResponseMixin. I am experimenting with this, as I am interested in creating a generic view that will do an object_detail view with a ModelForm so that my model rows can be generated automatically.

To try to duplicate the DetailView I tried to create a class as follows:

from django.views.generic import list_detail, View
from django.views.generic.detail import (SingleObjectMixin,
    SingleObjectTemplateResponseMixin, BaseDetailView)

class formdisplay(View,SingleObjectMixin,SingleObjectTemplateResponseMixin): pass

When I use formdisplay instead of list_detail.object_detail I get the error

TypeError at /inpatient-detail/4/
__init__() takes exactly 1 non-keyword argument (2 given)

Any hints at how to do this?

Also, where is the documentation on how to write the import statements? I had to google to find what to import from as I couldn't find that in the documentation.

Thanks in advance,
Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2024-11-24 23:11:59

由于 django 关于基于类的泛型视图的文档仍然不是很先进,因此获得有关它们的更多信息的最佳方法是浏览源代码;对于创建/更新视图是一个很好的选择开始。

当从多个类/mixin 继承时,您还应该注意它们的顺序 - 如果您查看 django 的源代码,您会发现它们将 Mixins 放在其他类之前!

我并不完全清楚您想要实现的目标,但如果您的目标是显示包含现有对象数据的表单,django.views.generic.update.UpdateView 应该是你的朋友!

As django's documentation on class-based generic view is still not very state-of-the-art, the best thing to get more information about them is to browse the source code; for create/update views this is a good start.

When inheriting from multiple classes/mixins you should also keep an eye on their order - if you look at django's source you'll see they put the Mixins before the other classes!

It's not totally clear to me, what you are trying to achieve, but if your goal is to display a form with data from an existing object, django.views.generic.update.UpdateView should be your friend!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文