Advertisement
Darkolius

Untitled

Aug 17th, 2021
1,358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. class UserSignUpForm(forms.ModelForm):
  2.     def __init__(self, *args, **kwargs):
  3.         super(UserSignUpForm, self).__init__(*args, **kwargs)
  4.         self.fields['email'].required = True
  5.         self.fields['first_name'].required = True
  6.         self.fields['last_name'].required = True
  7.  
  8.     who = forms.ChoiceField(
  9.         choices=[('student', 'Student'), ('teacher', 'Teacher')],
  10.         label="",
  11.         required=True,
  12.         widget=forms.RadioSelect(
  13.             attrs={'style':'max-width: 20em; ', 'autocomplete':'off', })
  14.     )
  15.     password = forms.CharField(
  16.         label="Password",
  17.         help_text="Your password must contain at least 8 characters and can’t be entirely numeric.",
  18.         validators=[MinLengthValidator(8, message="Minimum 8 characters"),],
  19.         widget=forms.PasswordInput(attrs={'autocomplete':'off'}))
  20.     confirm_password = forms.CharField(
  21.         label="Confirm password",
  22.         validators=[MinLengthValidator(8, message="Minimum 8 characters"), ],
  23.         widget=forms.PasswordInput(attrs={'autocomplete':'off'}))
  24.  
  25.     class Meta:
  26.         model = User
  27.         fields = ('who', 'username', 'email', 'first_name', 'last_name', 'password', )
  28.         help_texts = {"password": None}
  29.         widgets = {
  30.             'username': forms.TextInput(attrs={}),
  31.             'first_name': forms.TextInput(attrs={}),
  32.             'last_name': forms.TextInput(attrs={}),
  33.             'email': forms.TextInput(attrs={}),
  34.         }
  35.  
  36.     def clean(self):
  37.         cleaned_data = super(UserSignUpForm, self).clean()
  38.         password = cleaned_data.get("password")
  39.         NumericPasswordValidator(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)
  45.  
  46.  
  47.     helper = FormHelper()
  48.     helper.form_tag = 'false'
  49.     helper.attrs = {"novalidate": True, 'autocomplete':'off'}
  50.     helper.form_class = 'form-horizontal'
  51.     helper.field_class = 'col-md-7 '
  52.     helper.label_class = 'col-md-5'
  53.     helper.layout = Layout(
  54.         Row(
  55.             Column(
  56.                 Field('who', css_class='form-group', style='margin-left:200px'),
  57.                 Field('username', css_class='form-group ', style=''),
  58.                 Field('email', css_class='form-group'),
  59.                 Field('first_name', css_class='form-group'),
  60.                 Field('last_name', css_class='form-group'),
  61.                 Field('password', css_class='form-group'),
  62.                 Field('confirm_password', css_class='form-group'),
  63.  
  64.                 FormActions(
  65.                     Submit('save', 'Sign up', css_class="btn-primary"),
  66.  
  67.                     ),
  68.  
  69.             )
  70.         )
  71.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement