在Django中滚动自己的通用视图 [英] Rolling your own generic views in Django

查看:166
本文介绍了在Django中滚动自己的通用视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

尝试复制DetailView我试图创建一个类如下:

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

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

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?

另外,关于如何编写import语句的文档在哪里?我不得不谷歌找到要导入的东西,因为我在文档中找不到。

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.

提前感谢
Steve

Thanks in advance, Steve

推荐答案

由于django的基于类的通用视图的文档仍然不是最先进的,最好的事情是获取更多关于它们的信息是浏览源代码;用于创建/更新视图 this 是一个很好的开始。

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.

当从多个类/ mixins继承时,您还应该关注他们的订单 - 如果你看看django的来源,你会看到他们把Mixins >之前其他课程!

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!

对我来说,你想要实现的还不是很清楚,但如果你的目标是显示一个数据表单从现有对象, django.views.generic.update.UpdateView 应该是你的朋友!

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!

这篇关于在Django中滚动自己的通用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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