class RegistrationFormPreview(FormPreview):
preview_template = 'workshops/workshop_register_preview.html'
form_template = 'workshops/workshop_register_form.html'
def done(self, request, cleaned_data):
# Do something with the cleaned_data, then redirect
# to a "success" page.
# data = request.POST.copy()
# data['user_id'] = u'%s' % (request.user.id)
# cleaned_data['user'] = u'%s' % (request.user.id)
#f = self.form(cleaned_data)
#f = self.form(data)
#f.user = request.user
f = self.form(request.POST)
f.save()
pdb.set_trace()
return HttpResponseRedirect('/register/success')
正如你所看到的,我已经尝试了几种方法,并且已经被注释掉了。任务显然很简单:在保存之前将用户从请求添加到表单,然后保存。
这里接受的工作方法是什么?
答案 0 :(得分:12)
如果用户无法修改,我会说它首先不应该包含在表格中。
无论哪种方式,using the commit
argument都可以防止生成的对象立即保存(假设FormPreview
使用ModelForm
):
obj = form.save(commit=False)
obj.user = request.user
obj.save()