【发布时间】:2020-05-01 04:08:41
【问题描述】:
我有一个表单,我想将它注入到基于类的 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
很遗憾,PastLocationForm 表单在注入后没有出现在 HTML 页面上。我检查了页面,什么都没有。
有趣的是,如果我将 PastLocationForm 传递给功能视图并将其呈现给另一个页面,表单就会显示出来!我还有其他视图,我将“form_class”用于其他 modelForms,它们可以正常工作。
如果找不到解决方案,我会将视图切换到功能视图,但我宁愿保留基于类的视图。
【问题讨论】:
标签: django