由于形式较长,我为它创建了一个ModelForm向导,因此用户可以完成步骤1和2.实现后,表单数据不会保存在数据库中。以下是我的代码。
模型
class FimpForm1(ModelForm):
class Meta:
model=Fimp
fields=('who_are_you','name','main_view','side_view','other_1_view','other_2_view','other_3_view','other_4_view','other_5_view','other_6_view','other_7_view','other_8_view')
class FimpForm2(forms.ModelForm):
details=forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
class Meta:
model=Fimp
fields=('address','city','state','email','phone_no','details','price')
exclude=('user','pub_date','slug')
Views.py
class FimpWizard(SessionWizardView):
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'photos'))
def done(self, form_list, **kwargs):
form=FimpForm1 and FimpForm2(self.request.POST,self.request.FILES)
if form.is_valid():
data=form.cleaned_data
newfimp=Fimp(
user=request.user,
pub_date=datetime.datetime.now(),
who_are_you=data['who_are_you'],
name=data['name'],
main_view=request.FILES.get('main_view'),
side_view=request.FILES.get('side_view'),
other_1_view=request.FILES.get('other_1_view'),
other_2_view=request.FILES.get('other_2_view'),
other_3_view=request.FILES.get('other_3_view'),
other_4_view=request.FILES.get('other_4_view'),
other_5_view=request.FILES.get('other_5_view'),
other_6_view=request.FILES.get('other_6_view'),
other_7_view=request.FILES.get('other_7_view'),
other_8_view=request.FILES.get('other_8_view'),
address=data['address'],
city=data['city'],
state=data['state'],
email=data['email'],
phone_no=data['phone_no'],
details=data['details'],
price=data['price'])
newfimp.save()
return HttpResponseRedirect('/view_list/')
答案 0 :(得分:0)
您的done()
方法不正确。调用done()
方法时,django已经验证了数据,您只需要保存表单。
e.g。
def done(self, form_list, **kwargs):
#you can access form as
form1 = form_list[0]
obj1 = form1.save(commit=False)
#now set fields from 2nd form
form2 = form_list[1]
obj1.user = self.request.user
obj1.address = form2.cleaned_data['address']
...
obj1.save()
return HttpResponseRedirect('/view_list/')