Advertisement
Darkolius

Untitled

May 3rd, 2021
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from django.core.validators import MinLengthValidator
  4. from django.utils.translation import  gettext_lazy as _
  5.  
  6. #https://docs.djangoproject.com/en/3.1/ref/forms/widgets/
  7.  
  8.  
  9. class UserSignUpForm(forms.ModelForm):
  10.     who = forms.ChoiceField(
  11.         choices=[('student', 'Student'), ('teacher', 'Teacher')],
  12.         required=True,
  13.         widget=forms.RadioSelect(
  14.             attrs={'style':'max-width: 20em; margin:auto;' ,
  15.                    'autocomplete':'off', })
  16.     )
  17.     password = forms.CharField(
  18.         label="Password",
  19.         validators=[MinLengthValidator(8, message="Minimum 8 characters")],
  20.         widget=forms.PasswordInput(attrs={'style':'max-width: 20em; margin:auto', 'autocomplete':'off'}))
  21.     confirm_password = forms.CharField(
  22.         label="Confirm password",
  23.         validators=[MinLengthValidator(8, message="Minimum 8 characters"), ],
  24.         widget=forms.PasswordInput(attrs={'style':'max-width: 20em; margin:auto', 'autocomplete':'off'}))
  25.  
  26.     class Meta:
  27.         model = User
  28.         fields = ('who', "username", 'first_name', 'last_name', "password", )
  29.         help_texts = {"username": None}
  30.         widgets = {
  31.             'username': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),
  32.             'first_name': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),
  33.             'last_name': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),
  34.  
  35.         }
  36.  
  37.     def clean(self):
  38.         cleaned_data = super(UserSignUpForm, self).clean()
  39.         password = cleaned_data.get("password")
  40.         confirm_password = cleaned_data.get("confirm_password")
  41.         if password != confirm_password:
  42.             msg = _(f'Password and confirm password does not match')
  43.             self.add_error('password', msg)
  44.             self.add_error('confirm_password', msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement