2013-10-27 78 views
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') 

以下是輸出:

打印profile_form.is_bound - 返回True和 打印profile_form.is_valid()返回false。也中沒有誤差修改{{profile_form.errors}} { {profile_form.non_field_errors}}

感謝您的答覆... 在此先感謝...

回答

0

我想通了......

我只包含表單定義

fields = ('','',) 

感謝的字段。

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') 
+0

感謝以下:)噢,我的.... –