字段的清除值未在表单输入中显示(仅显示旧值)

时间:2012-12-26 18:03:18

标签: python django django-forms

我有以下表格:

class SourceForm(forms.ModelForm):
    class Meta:
        model = Source
        widgets = {
            'category_re': forms.TextInput(),
            'thumb_re': forms.TextInput(),
            'movie_re': forms.TextInput()
        }

    def clean_url(self):
        data = self.cleaned_data['url']
        return helpers.url_fix(data)

和以下观点:

def source_form(request, id):
    source = get_object_or_404(Source, pk=id)
    form = SourceForm(request.POST or None, instance=source)

    if form.is_valid():
        form.save()

    return render_to_response('source/form.html', {'form': form, 'source': source},
        context_instance=RequestContext(request))

我有问题。表格中未显示url字段的清除值。我只看到旧的,未修改的值,但后来我尝试在模板中添加{{ form.cleaned_data }} - 它显示正确的值。为什么会这样?我该如何解决?

TIA!

1 个答案:

答案 0 :(得分:2)

这是因为您的form使用的是request.POST数据,而不是cleaned_data。尝试在form

之后重新分配save()变量
def source_form(request, id):
    source = get_object_or_404(Source, pk=id)
    form = SourceForm(request.POST or None, instance=source)

    if form.is_valid():
        source = form.save()
        form = SourceForm(instance=source)

    return render_to_response('source/form.html', {'form': form, 'source': source},
        context_instance=RequestContext(request))