У меня есть профиль пользователя и вы хотите, чтобы пользователь обновлял все о себе (имя пользователя, пароль, имя и другие данные). В приведенном ниже коде поля password1 и password2 не загружаются, а метод clean_username не вызывается. Чтобы закончить, когда я вызываю метод is_valid, он всегда возвращает False, но без ошибок.Django: поля не загружаются в формы
Может ли кто-нибудь мне помочь? Спасибо.
#forms.py
class UserProfileForm(ModelForm):
c_user = None
error_messages = {
'duplicate_username': _("A user with that username already exists."),
'password_mismatch': _("The two password fields didn't match."),
}
username = forms.RegexField(label=_("Username"), max_length=30,
regex=r'^[\[email protected]+-]+$',
help_text = _("Required. 30 characters or fewer. Letters, digits and "
"@/./+/-/_ only."),
error_messages = {
'invalid': _("This value may contain only letters, numbers and "
"@/./+/-/_ characters.")})
def __init__(self, *args, **kwargs):
super(UserProfileForm, self).__init__(*args, **kwargs)
if kwargs.has_key('instance'):
current_user = kwargs.get('instance').user
self.c_user = current_user
self.password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput, required = False)
self.password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput, required = False,
help_text = _("Enter the same password as above, for verification."))
else:
self.password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput)
self.password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput,
help_text = _("Enter the same password as above, for verification."))
def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"]
if (self.c_user == None) or (self.c_user.username != username):
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])
def clean_password2(self):
password1 = self.cleaned_data.get("password1", "")
password2 = self.cleaned_data["password2"]
if password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'])
return password2
class Meta:
model = UserProfile
exclude=('user')
>
#views.py
@csrf_protect
def update_user(request):
user = request.user
user_profile = user.get_profile()
if request.method == 'GET':
initials = {'username':user.username,
'email':user.email,
'name':user_profile.name,
'birth_date':user_profile.birth_date,
'weight':user_profile.weight,
'height':user_profile.height,
'smoke':user_profile.smoke,
'drink_alcohol':user_profile.drink_alcohol,
'alergies':user_profile.alergies,
}
form = UserProfileForm(initial=initials)
elif request.method == 'POST':
form = UserProfileForm(request.POST, instance=user_profile)
if form.is_valid():
f = form.save(commit=False)
user.username = request.POST.get('username')
user.save()
f.user = user
f.save()
print request.POST.get('username')
return redirect('/')
return render_to_response('profile.html', {'form':form}, context_instance=RequestContext(request))
UPDATE: Спасибо Daniel Роземан! Теперь у меня проблема только с полями password1 и password2. Они не загружаются.
Почему вы посылаете form.asp_p() в шаблоне ? Просто отправьте объект формы в шаблон и получите его как form.as_p() в шаблоне –