У меня проблема с предварительным заполнением форм в Джанго. Поскольку инструмент отладки в Aptana не работает должным образом, я не смог обнаружить ошибку.
Я думаю, что при попытке form = MemberSettings(default_data)
форма пытается проверить данные и выдает ошибки для недопустимых полей даже при первой загрузке.
Не могли бы вы помочь мне выяснить проблему?
Спасибо
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})