0

我有一个配置文件更新表单,我已将数据和实例传递给表单但验证失败场景如下。

此表单用于配置文件更新,但验证失败且未显示任何错误。

 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 }} 中也没有错误

感谢您的回复...在此先感谢...

4

2 回答 2

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')
于 2013-10-27T08:35:52.300 回答
0

我想通了 ...

我只是在表单定义中包含了这些字段

fields = ('','',) 

谢谢。

于 2013-10-27T08:35:30.030 回答