Форум сайта python.su
Есть форма:
class RegisterForm(models.Form): username = forms.CharField() password = forms.CharField() password_confirm = forms.CharField()
def valid_password_confirm(password, confirm): if password != confirm: raise forms.ValidationError("Passwords are different.")
class RegisterForm(models.Form): username = forms.CharField() password = forms.CharField() password_confirm = forms.CharField() def clean(self): cleaned_data = super().clean() valid_password_confirm( cleaned_data["password"], cleaned_data["password_confirm"] )
Отредактировано Podarok (Июль 28, 2018 00:19:36)
Офлайн
Написал так:
class RegisterForm(models.Form): username = forms.CharField() password = forms.CharField() password_confirm = forms.CharField() def clean(self): cleaned_data = super().clean() if cleaned_data["password"] != cleaned_data["password_confirm"]: raise forms.ValidationError("Passwords are different.")
class RegisterView(views.View): def post(self, request): form = RegisterForm(request.POST) if form.is_valid(): # Сохранение юзера в бд return render(request, "register.html", context={"form": form})
<form method="POST"> {% csrf_token %} <div class="form-group"> {{ form.username.label_tag }} {{ form.username }} </div> <div class="form-group"> {{ form.password.label_tag }} {{ form.password }} </div> <div class="form-group"> {{ form.password_confirm.label_tag }} {{ form.password_confirm }} </div> </form>
Отредактировано Podarok (Июль 28, 2018 22:02:41)
Офлайн
После изменений в форме:
class RegisterForm(models.Form): username = forms.CharField() password = forms.CharField() password_confirm = forms.CharField() def clean(self): cleaned_data = super().clean() if cleaned_data["password"] != cleaned_data["password_confirm"]: raise forms.ValidationError("Passwords are different.")
class RegisterView(views.View): def post(self, request): form = RegisterForm(request.POST) if form.is_valid(): # Сохраниение юзера в бд return render(request, "register.html", context={"form": form}) else: render(request, "register.html", context={"form": form})
<form method="POST"> {% csrf_token %} {{ form }} <button type="submit" class="btn btn-default">Submit</button> </form>
if cleaned_data["password"] != cleaned_data["password_confirm"]: self.add_error("password_confirm", forms.ValidationError("Passwords are different."))
<form method="POST"> {% csrf_token %} <div class="form-group"> {{ form.username.label_tag }} {{ form.username }} </div> <div class="form-group"> {{ form.password.label_tag }} {{ form.password }} </div> <div class="form-group"> {{ form.password_confirm.label_tag }} {{ form.password_confirm }} </div> <button type="submit" class="btn btn-default">Submit</button> </form>
Отредактировано Podarok (Июль 29, 2018 12:24:06)
Офлайн
Правильно было так:
class RegisterForm(models.Form): username = forms.CharField() password = forms.CharField() password_confirm = forms.CharField() def clean(self): cleaned_data = super().clean() if cleaned_data["password"] != cleaned_data["password_confirm"]: raise forms.ValidationError("Passwords are different.")
class RegisterView(views.View): def post(self, request): form = RegisterForm(request.POST) if form.is_valid(): # Сохраниение юзера в бд return render(request, "register.html", context={"form": form}) else: render(request, "register.html", context={"form": form})
<form method="POST"> {% csrf_token %} {{ form.non_field_errors }} <div class="form-group"> {{ form.username.label_tag }} {{ form.username }} {{ form.username.errors }} </div> <div class="form-group"> {{ form.password.label_tag }} {{ form.password }} {{ form.password.errors }} </div> <div class="form-group"> {{ form.password_confirm.label_tag }} {{ form.password_confirm }} {{ form.password_confirm.errors }} </div> <button type="submit" class="btn btn-default">Submit</button> </form>
Отредактировано Podarok (Июль 30, 2018 13:08:29)
Офлайн