0

我有以下表格:

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 }}在模板中添加 - 它显示了正确的值。为什么会这样?我该如何解决?

蒂亚!

4

1 回答 1

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))
于 2012-12-26T18:58:34.853 回答