Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django import forms
- from django.contrib.auth.models import User
- from django.core.validators import MinLengthValidator
- from django.utils.translation import gettext_lazy as _
- #https://docs.djangoproject.com/en/3.1/ref/forms/widgets/
- from crispy_forms.helper import FormHelper
- from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field, Column
- from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions
- class UserSignUpForm(forms.ModelForm):
- def __init__(self, *args, **kwargs):
- super(UserSignUpForm, self).__init__(*args, **kwargs)
- self.fields['first_name'].required = True
- self.fields['last_name'].required = True
- who = forms.ChoiceField(
- choices=[('student', 'Student'), ('teacher', 'Teacher')],
- label="",
- required=True,
- widget=forms.RadioSelect(
- attrs={'style':'max-width: 20em; ', 'autocomplete':'off', })
- )
- password = forms.CharField(
- label="Password",
- validators=[MinLengthValidator(8, message="Minimum 8 characters")],
- widget=forms.PasswordInput(attrs={'autocomplete':'off'}))
- confirm_password = forms.CharField(
- label="Confirm password",
- validators=[MinLengthValidator(8, message="Minimum 8 characters"), ],
- widget=forms.PasswordInput(attrs={'autocomplete':'off'}))
- class Meta:
- model = User
- fields = ('who', "username", 'first_name', 'last_name', "password", )
- help_texts = {"username": None}
- widgets = {
- 'username': forms.TextInput(attrs={}),
- 'first_name': forms.TextInput(attrs={}),
- 'last_name': forms.TextInput(attrs={}),
- }
- def clean(self):
- cleaned_data = super(UserSignUpForm, self).clean()
- password = cleaned_data.get("password")
- confirm_password = cleaned_data.get("confirm_password")
- if password != confirm_password:
- msg = _(f'Password and confirm password does not match')
- self.add_error('password', msg)
- self.add_error('confirm_password', msg)
- helper = FormHelper()
- helper.form_tag = 'false'
- helper.attrs = {"novalidate": True, 'autocomplete':'off'}
- helper.form_class = 'form-horizontal'
- helper.field_class = 'col-md-8 '
- helper.label_class = 'col-md-4'
- helper.layout = Layout(
- Row(
- Column(
- Field('who', css_class='form-group', style='margin-left:200px'),
- Field('username', css_class='form-group ', style=''),
- Field('first_name', css_class='form-group'),
- Field('last_name', css_class='form-group'),
- Field('password', css_class='form-group'),
- Field('confirm_password', css_class='form-group'),
- FormActions(
- Submit('save', 'Sign up', css_class="btn-primary"),
- Submit('cancel', 'Cancel'),
- ),
- )
- )
- )
- class UserLoginForm(forms.Form):
- username = forms.CharField(widget=forms.TextInput(
- attrs={
- "class": "form-control"
- }))
- password = forms.CharField(
- widget=forms.PasswordInput(
- attrs={
- "class": "form-control",
- "id": "user-password"
- }
- )
- )
- def clean_username(self):
- username = self.cleaned_data.get("username")
- qs = User.objects.filter(username__iexact=username) # thisIsMyUsername == thisismyusername
- if not qs.exists():
- raise forms.ValidationError("This is an invalid user.")
- if qs.count() != 1:
- raise forms.ValidationError("This is an invalid user.")
- return username
- helper = FormHelper()
- helper.form_tag = 'false'
- helper.attrs = {'autocomplete':'off'}
- helper.form_class = 'form-horizontal'
- helper.field_class = 'col-md-8 '
- helper.label_class = 'col-md-4'
- helper.layout = Layout(
- Row(
- Column(
- Field('username', css_class='form-group ', style=''),
- Field('password', css_class='form-group'),
- FormActions(
- Submit('submit', 'Sign in', css_class="btn-primary"),
- Submit('cancel', 'Cancel'),
- ),
- )
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement