1

我想在用户单击预览后但在再次显示给他们之前更改从表单收集的数据。

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 方法玩了很多,但什么都做不了。

4

1 回答 1

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

升级版:

我发现只有使用 form clean 方法的解决方案:

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 %}
于 2012-12-20T10:26:40.127 回答