我想在用户点击预览之后但在再次向他们展示之前更改从表单收集的数据。
class StoryForm(forms.Form):
title = forms.CharField()
story = forms.CharField(widget=forms.Textarea)
class StoryFormPreview(FormPreview):
def done(self, request, cleaned_data):
# Do something with the cleaned_data, then redirect
# to a "success" page.
return HttpResponseRedirect('/form/success')
在显示预览之前,我想附加到用户为故事字段输入的任何内容,并添加“由某某人带给您”。我该怎么办呢?我使用process_preview和preview_post方法玩了很多但是无法正常工作。
答案 0 :(得分:0)
据我了解,您可以使用
进行修改def process_preview(self, request, form, context):
"""
Given a validated form, performs any extra processing before displaying
the preview page, and saves any extra data in context.
"""
pass
UPD:
我发现只有表单清理方法的解决方案:
class StoryForm(forms.Form):
...
def clean_story(self):
self.cleaned_data['story'] = ...
return self.cleaned_data['story']
class StoryFormPreview(FormPreview):
def process_preview(self, request, form, context):
preview_data = {}
for key, value in form.cleaned_data.iteritems():
preview_data[key] = value
context['preview_data'] = preview_data
formtools/preview.html
{% for label, data in preview_data.iteritems %}
<tr>
<th>{{ label }}:</th>
<td>{{ data }}</td>
</tr>
{% endfor %}