我有一个配置文件更新表单,我已将数据和实例传递给表单,但验证失败。方案如下。
此表单用于配置文件更新,但验证失败但未显示任何错误。
class ProfileEditForm(forms.ModelForm):
class Meta:
model = Profile
def clean(self):
return self.cleaned_data
视图如下
> form.is_valid始终返回False。
def user_profile(request, params={}):
if request.user.is_staff==False:
profile = Profile.objects.get(user=request.user)
if request.method == 'POST':
profile_form = ProfileEditForm(request.POST, instance=profile)
print profile_form.is_bound
print profile_form.is_valid()
if profile_form.is_valid():
profile = profile_form.save()
else:
pass
else:
profile_form = ProfileEditForm( instance=profile)
profile_form = ProfileEditForm( instance=profile)
params['profile_form'] = profile_form
return render_to_response('vec/profile.html', params, context_instance=RequestContext(request))
else:
return render(request, 'base.html')
以下是产出:
print profile_form.is_bound - 返回True和 print profile_form.is_valid()返回False。{{profile_form.errors}} {{profile_form.non_field_errors}}
中没有错误。
感谢您的回复...... 提前谢谢......
答案 0 :(得分:2)
您在重置profile_form
之前将其发送到模板,因此未显示任何错误。
见下面的评论
def user_profile(request, params={}):
if request.user.is_staff==False:
profile = Profile.objects.get(user=request.user)
if request.method == 'POST':
profile_form = ProfileEditForm(request.POST, instance=profile)
print profile_form.is_bound
print profile_form.is_valid()
if profile_form.is_valid():
profile = profile_form.save()
else:
pass
else:
profile_form = ProfileEditForm( instance=profile)
# don't do this, you already have profile_form
#profile_form = ProfileEditForm( instance=profile)
params['profile_form'] = profile_form
return render_to_response('vec/profile.html', params, context_instance=RequestContext(request))
else:
return render(request, 'base.html')
答案 1 :(得分:0)
我想通了......
我刚刚在表单定义中包含了字段
fields = ('','',)
感谢。