问题
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
回答1:
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!
来源:https://stackoverflow.com/questions/6529929/rolling-your-own-generic-views-in-django