Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

django - Inherit from parent class and form, then i want a listview

//i have a modelform

class Parrent(forms.ModelForm):
    someTest = forms.CharField(max_length=20)

// i have a child form

class Child(Parrent):
    someTestChild = forms.CharField(max_length=20)

// i want a listview for the child form, this is my url.py

path('Testing/', 
views.Child.as_view(),name="child"),

//but i get an error saying that this class has no .as_view possibility. Is it possible to create an list and detailview on the child form.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are calling as_view() on the forms.ModelForm class which is not possible. You can only call that on a Class-Based View. Also, do you even have a model created that you can use for the form and view?

Consult the following docs for an example ListView (and also check the docs about models if you don't have any yet): https://docs.djangoproject.com/en/3.1/ref/class-based-views/generic-display/

Note the difference between a view and a form in general:

from django.views.generic.detail import DetailView

from articles.models import Article

class ArticleDetailView(DetailView):
    model = Article

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...