2011-06-19 54 views
0

我在Django中的表單預填充時遇到了麻煩。由於Aptana中的調試工具無法正常工作,因此無法檢測到錯誤。 我認爲當我嘗試form = MemberSettings(default_data)表單試圖驗證數據,並且即使在第一次加載時它也會爲無效字段提供錯誤。在使用Django時Django表單編輯問題

你能幫我找出問題嗎?

感謝

forms.py

class MemberSettings(forms.Form): 
email = forms.EmailField(label=_("member_Email"),required = True) 
currentPassword = forms.CharField(label=_("member_currentPassword"),widget=forms.PasswordInput,required=False) 
newPassword = forms.CharField(label=_("member_newPassword"),widget=forms.PasswordInput,required=False) 
newPasswordRe = forms.CharField(label=_("member_newPasswordRe"),widget=forms.PasswordInput,required=False) 
emailPreference = forms.ChoiceField(label=_("member_email_preference"), widget=forms.RadioSelect(renderer=HorizRadioRenderer),choices = UserMailPreference.USER_MAIL_PREF,required = True) 
gender = forms.ChoiceField(label=_("member_gender"), widget=forms.RadioSelect(renderer=HorizRadioRenderer),choices = UserGender.USER_GENDER ,required = False) 
birthYear = forms.ChoiceField(label=_("member_birthyear"),required = False) 
education = forms.ChoiceField(label=_("member_education"),choices = UserEducation.USER_EDU, required = False) 

def __init__(self,*args, **kwargs): 
    super(MemberSettings,self).__init__(*args, **kwargs) 
    now = datetime.datetime.now() 

    birthYearList = [] 
    for i in reversed(range (now.year-80,now.year-13)): 
     birthYearList.append((i,str(i))) 
    self.fields["birthYear"].choices = birthYearList 

def clean_currentPassword(self): 
    field_data = self.cleaned_data['currentPassword'] 

    if len(field_data) <= 0: 
     return field_data 

    if len(field_data.split(' ')) != 1: 
     raise forms.ValidationError(_('member_err_password_empty_char')) 

    if len(field_data) > 32: 
     raise forms.ValidationError(_('member_err_password_maxChar')) 

    if len(field_data) < 5: 
     raise forms.ValidationError(_('member_err_password_minChar')) 

    return field_data 

def clean_newPassword(self): 
    field_data = self.cleaned_data['newPassword'] 

    if len(field_data) <= 0: 
     return field_data 

    if len(field_data.split(' ')) != 1: 
     raise forms.ValidationError(_('member_err_password_empty_char')) 

    if len(field_data) > 32: 
     raise forms.ValidationError(_('member_err_password_maxChar')) 

    if len(field_data) < 5: 
     raise forms.ValidationError(_('member_err_password_minChar')) 

    return field_data 

views.py

@login_required 
def settings_edit(request): 
u = request.user 
if request.method == 'POST': 
    form = MemberSettings(request.POST) 
    if form.is_valid(): 

     return redirect('/member/settings_edit/') 
    else: 
     return render_response(request, 'member/settings_edit.html', {'form': form}) 
else: 
    default_data = { 
      'email': u.email, 
      'gender': u.get_profile().gender, 
      'education': u.get_profile().education, 
      'birthYear': u.get_profile().birthyear, 
      'emailPreference': u.get_profile().mail_preference, 
      } 
    form = MemberSettings(default_data) 
    return render_response(request, 'user/settings_edit.html', {'form': form}) 

回答