我正在寻找在Django应用程序中复制以下表单结构的方法。这些已经在PHP应用程序中进行了编码。
在第一页上有两种单独的输入表单,其中顶部表单与底部表单略有不同。目前,它们是form.py下的两个单独的表单,但可以组合在一起:
forms.py:
# forms.py
from django import forms
from .models import Data2, Data3
class ExampleEntryFormTop(forms.Form):
example_date = forms.DateField(widget=forms.HiddenInput())
data_top = forms.IntegerField()
data2 = forms.ModelChoiceField(Data2.objects, to_field_name="name", empty_label=None, widget=forms.RadioSelect)
additional = forms.MultipleChoiceField(choices=(
('one', 'One'),
('two', 'Two'),
('three', 'Three')),
widget=forms.CheckboxSelectMultiple
)
data3 = forms.ModelChoiceField(Data3.objects, empty_label=None, widget=forms.RadioSelect)
data4 = forms.MultipleChoiceField(choices=(
('alpha', 'Alpha'),
('beta', 'Beta'),
('gamma', 'Gamma')),
widget=forms.CheckboxSelectMultiple
)
class ExampleEntryFormBottom(forms.Form):
example_date = forms.DateField(widget=forms.HiddenInput())
data_bottom = forms.IntegerField()
data2 = forms.ModelChoiceField(Data2.objects, to_field_name="name", empty_label=None, widget=forms.RadioSelect)
additional = forms.MultipleChoiceField(choices=(
('one', 'One'),
('two', 'Two'),
('three', 'Three')),
widget=forms.CheckboxSelectMultiple
)
data3 = forms.ModelChoiceField(Data2.objects, empty_label=None, widget=forms.RadioSelect)
class ExampleEntryFormPage2(forms.Form):
example_date = forms.DateField(widget=forms.HiddenInput())
data_top = forms.IntegerField()
data_bottom = forms.IntegerField()
data2 = forms.ModelChoiceField(Data2.objects, to_field_name="name", empty_label=None, widget=forms.RadioSelect)
additional = forms.MultipleChoiceField(choices=(
('one', 'One'),
('two', 'Two'),
('three', 'Three')),
widget=forms.CheckboxSelectMultiple
)
data3 = forms.ModelChoiceField(Data2.objects, empty_label=None, widget=forms.RadioSelect)
在向导的第二页上,我需要预加载接下来的三天。我一直在使用以下内容来创建表单集:
forms.py:
def get_example_set():
initial_date = date.today()
example_days = [initial_date + timedelta(days=x) for x in range(2, 5)]
ExampleFormset = formset_factory(ExampleEntryFormPage2, max_num=len(example_days))
form_set = ExampleFormset(initial=[{'example_date': x} for x in example_days])
return form_set
但是,我能够将此表单集传递到SessionWizardView的唯一方法是使用get_context_data,它可以传入表单集,但感觉不正确:
views.py:
from formtools.wizard.views import SessionWizardView
from .forms import get_example_set
from django.shortcuts import render
from datetime import date, timedelta
class ExampleWizardView(SessionWizardView):
template_name = 'example_entry_wizard.html'
def done(self, form_list, **kwargs):
return render(self.request, 'done.html', {
'form_data': [form.cleaned_data for form in form_list],
})
def get_form(self, step=None, data=None, files=None):
form = super(ExampleWizardView, self).get_form(step, data, files)
step = step or self.steps.current
form = get_example_set()
return form
def get_context_data(self, **kwargs):
# FIXME: Make this time-zone aware
context = super(ExampleWizardView, self).get_context_data(**kwargs)
initial_date = date.today()
weekday = initial_date.strftime("%A")
day_one_example = initial_date + timedelta(days=1)
context['initial_date'] = initial_date
context['weekday'] = weekday
context['day_one_example'] = day_one_example
context['formset'] = get_example_set()
context['formset_type'] = type(get_example_set())
return context
def form_valid(self, form):
form.save_example()
return super().form_valid(form)
我觉得我正在努力使用此工具,但我不确定如何最好地使用它。到目前为止,我所看到的示例建议修改get_form
使其正常工作,但是我不确定如何针对这些类型的表单进行操作。
似乎每当我尝试使用get_example_set函数设置表单集时都会遇到问题,因为我没有FormSet类,而是有一个对象。
我也知道这里还没有第一页的代码。我仍在尝试找出将其添加到FormWizard中的最佳方法。