0

I have a form that I want to inject into a class based DetailView.

forms.py

class PastLocationForm(forms.Form):
    locations = forms.ModelChoiceField(queryset=Location.objects.all().order_by('location_name'))

views.py

class PatientDetailView(DetailView):
    model=Patient
    form_class = PastLocationForm

Unfortunately, the form PastLocationForm doesn't appear on the HTML page after injection. I inspected the page and there was nothing.

Interestingly, if I pass PastLocationForm to a functional view and render it for another page, the form shows up! I also have other views where I make use of "form_class" for other modelForms and they function correctly.

I will switch my view to functional view if I can't find the solution but I would rather keep the class based view.

1 Answers1

0

The reason might be the fact that DetailView does not handle a form_class attribute (FormViews do), you'd need to use a mixin.

Check out this answer:

Django combine DetailView and FormView

  • This is an interesting approach. I would have never though of it by myself –  May 01 '20 at 05:22