Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django import forms
- from signup.models import allusers1
- from django.contrib.auth import (
- authenticate,
- login,
- logout,
- get_user_model,
- )
- User=get_user_model()
- class UserLoginForm(forms.ModelForm):
- class Meta:
- model = allusers1
- fields=['username','password']
- widgets = {
- 'password': forms.PasswordInput(),
- }
- def clean(self ,*args,**kwargs):
- username=self.cleaned_data.get("username")
- password=self.cleaned_data.get("password")
- user_qs = User.objects.filter(username=username)
- if user_qs.count() == 0:
- raise forms.ValidationError("The user does not exist")
- else:
- if username and password:
- user = authenticate(username=username, password=password)
- if not user:
- raise forms.ValidationError("Incorrect password")
- if not user.is_active:
- raise forms.ValidationError("This user is no longer active")
- return super(UserLoginForm,self).clean(*args,**kwargs)
- from django.db import models
- from django import forms
- # Create your models here.
- class allusers1(models.Model):
- username=models.CharField(max_length=40)
- password=models.CharField(max_length=40)
- phoneno=models.CharField(max_length=10,primary_key=True)
- otp=models.IntegerField(blank=True,null=True,default=0000)
- def __str__(self):
- return self.username
- from django.shortcuts import render,redirect
- from .forms import UserLoginForm
- from django.contrib.auth import login as auth_loginwa
- from django.contrib.auth import (
- authenticate,
- logout,
- get_user_model,
- )
- def login(request):
- form1=UserLoginForm(request.POST or None)
- if form1.is_valid():
- username=form1.cleaned_data.get("username")
- password=form1.cleaned_data.get("password")
- user=authenticate(username=username,password=password)
- auth_loginwa(request,user)
- print(request.user.is_authenticated())
- return redirect("home2")
- context= {
- "form1": form1,
- }
- return render(request, "login.html",context)
- <form action="" method="POST">
- {% csrf_token %}
- {{ form1.as_p }}
- <input type="submit" value="SUBMIT"/>
- </form>
Add Comment
Please, Sign In to add comment