Advertisement
Darkolius

Untitled

May 1st, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 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(attrs={'style':'max-width: 20em; margin:auto; padding:300;', 'autocomplete':'off'})
  14.     )
  15.     password = forms.CharField(
  16.         label="Password",
  17.         validators=[MinLengthValidator(8, message="Minimum 8 characters")],
  18.         widget=forms.PasswordInput(attrs={'style':'max-width: 20em; margin:auto', 'autocomplete':'off'}))
  19.     confirm_password = forms.CharField(
  20.         label="Confirm password",
  21.         validators=[MinLengthValidator(8, message="Minimum 8 characters"), ],
  22.         widget=forms.PasswordInput(attrs={'style':'max-width: 20em; margin:auto', 'autocomplete':'off'}))
  23.  
  24.     class Meta:
  25.         model = User
  26.         fields = ('who', "username", 'first_name', 'last_name', "password", )
  27.         help_texts = {"username": None}
  28.         widgets = {
  29.             'username': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),
  30.             'first_name': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),
  31.             'last_name': forms.TextInput(attrs={'style':'max-width: 20em; margin:auto'}),
  32.  
  33.         }
  34.  
  35.     def clean(self):
  36.         cleaned_data = super(UserSignUpForm, self).clean()
  37.         password = cleaned_data.get("password")
  38.         confirm_password = cleaned_data.get("confirm_password")
  39.         if password != confirm_password:
  40.             msg = _(f'Password and confirm password does not match')
  41.             self.add_error('password', msg)
  42.             self.add_error('confirm_password', msg)
  43.  
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement