我正在尝试使用我的 Django FormWizard 编辑现有对象。我正在遵循此博客文章中描述的技术,但它不起作用。这是我的编辑视图:
@login_required
def edit_wizard(request, id):
thing = get_object_or_404(Thing, pk=id)
if thing.user != request.user:
raise HttpResponseForbidden()
else:
initial = {0: {'year': thing.year,
'make': thing.make,
'series': thing.series,
....etc.
},
1: {'condition': thing.condition,
....etc.
},
}
form = CreateWizard.as_view([StepOneForm, StepTwoForm, StepThreeForm], initial_dict=initial)
return form(context=RequestContext(request), request=request)
你能帮我弄清楚如何向向导提供初始数据,以便我可以允许用户编辑他们的对象吗?谢谢你的想法!
编辑:(2/18/13)
得到一个:
TypeError at /edit/10/ __init__() takes exactly 1 argument (3 given)
@sneawo 在下面的回答解决了这个问题,但仍然没有传递初始数据,而是向导创建了新对象。
编辑:(2/19/13)
class CreateWizard(SessionWizardView):
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT))
def done(self, form_list, **kwargs):
instance = Thing()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
setattr(instance, field, value)
instance.user = self.request.user
instance.save()
return render_to_response('wizard-done.html', {
'form_data': [form.cleaned_data for form in form_list],
})