Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- email=forms.EmailField(label='Email',max_length=254)
- password = forms.CharField(label='Password',
- widget=forms.PasswordInput())
- def clean(self,*args,**kwargs):
- email=self.self.cleaned_data.get("email")
- password=self.cleaned_data.get("password")
- if email and password:
- user=authenticate(email=email,password=password)
- if not user:
- raise forms.ValidationError("This email id is not registered")
- if not user.check_password(password):
- raise forms.ValidationError("Incorrect password")
- return super(UserLoginForm,self).clean(*args,**kwargs)
- error_messages = {
- 'duplicate_emailid': "This email id is already exists.",
- 'duplicate_mobile': "This mobile no is already exists.",
- 'password_mismatch': "The two password fields didn't match.",
- 'too_short': "Passwords must be at least 6 characters long.",
- }
- username= forms.CharField(label='Username', max_length=30)
- email = forms.EmailField(label='Email address',max_length=254)
- mobile =forms.CharField(label='Mobile No',max_length=10)
- password = forms.CharField(label='Password',
- widget=forms.PasswordInput())
- confirm_password = forms.CharField(label='Confirm Password',
- widget=forms.PasswordInput())
- class Meta:
- model = User
- fields = ['username', 'email', 'mobile', 'password','confirm_password']
- def username(self):
- username=self.cleaned_data.get("username")
- if username is None:
- msg="Plese Enter the User name"
- raise forms.ValidationError(msg)
- return username
- def clean_password(self):
- password = self.cleaned_data.get("password")
- if len(password) < 6:
- raise forms.ValidationError(
- self.error_messages['too_short'],
- )
- return password
- def clean_confirm_password(self):
- password1 = self.cleaned_data.get('password')
- confirm_password = self.cleaned_data.get('confirm_password')
- if password and confirm_password and password != confirm_password:
- raise forms.ValidationError(
- self.error_messages['password_mismatch']
- )
- return confirm_password
- def clean_email(self):
- email = self.cleaned_data.get('email')
- check_duplicate_email = User.objects.filter (email=email).exists()
- if check_duplicate_email:
- raise forms.ValidationError(
- self.error_messages['duplicate_emailid']
- )
- return email
- def clean_mobile(self):
- mobile=self.cleaned_data.get('mobile')
- check_duplicate_mobile=User.objects.filter(mobile=mobile).exists()
- if check_duplicate_mobile:
- raise forms.ValidationError(
- self.error_messages['duplicate_mobile']
- )
- return mobile
- title="Login"
- form=UserLoginForm(request.POST or None)
- if form.is_valid():
- email=form.cleaned_data.get("email")
- password = form.cleaned_data.get("password")
- user=authenticate(email=email,password=password)
- login(request,user)
- return render(request,"registration/signup.html",{"form":form,"title"=title})
- title="Signup"
- form = RegistrationForm(request.POST or None)
- if form.is_valid():
- user=form.save(commit=False)
- email = form.cleaned_data.get("email")
- password = form.cleaned_data.get("password")
- user=set_password(password)
- user.save()
- new_user = authenticate(email=user.email, password=password)
- login(request, new_user)
- return redirect("registration/register.html")
- return render(request, "registration/signup.html", {"form": form,"title"=title})
Add Comment
Please, Sign In to add comment