我有以下表格:
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!
答案 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))