Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.shortcuts import render,redirect
- from django.http import HttpResponse
- from .form import SignUp,UserLogin
- from .models import users
- from django.contrib.auth import authenticate,get_user_model,login,logout
- from django.contrib.auth.models import User
- def Userlogin(request):
- form=UserLogin()
- if request.method == 'POST':
- user_login = UserLogin(request.POST or None)
- if user_login.is_valid():
- username = request.POST.get('email', '')
- password = request.POST.get('password', '')
- getInfo=users.objects.get(email='example@mail.com')
- user=authenticate(email=username,password=password)
- if user is not None and user.is_active:
- login(request,user)
- return redirect('/')
- args={'form':form}
- return render(request,'html/login.html',args)
- from django import forms
- from .models import users
- class UserLogin(forms.Form):
- email = forms.CharField(widget=forms.EmailInput(attrs=
- { 'value':'',
- 'class':'form-control'}))
- password = forms.CharField(widget=forms.TextInput(attrs=
- { 'type': '',
- 'value':"mukul",
- 'class':'form-control'}))
- from django.db import models
- class users(models.Model):
- id=models.AutoField(primary_key=True)
- name=models.CharField(max_length=100, blank=False)
- email=models.EmailField(max_length=100, blank=False, unique=True)
- password=models.CharField(max_length=500, blank=False)
- created_at=models.DateField(auto_now_add=True)
- updated_at=models.DateField(auto_now_add=True)
- def __str__(self):
- return self.email
- getInfo = users.objects.get(email='example@mail.com')
- from django.shortcuts import render,redirect
- from django.http import HttpResponse
- from .forms import SignUp,UserLogin #Wrong Import
- from .models import users
- from django.contrib.auth import authenticate,get_user_model,login,logout
- from django.contrib.auth.models import User
- def Userlogin(request):
- form=UserLogin()
- if request.method == 'POST':
- user_login = UserLogin(request.POST or None) #Indentation
- if user_login.is_valid():
- username = request.POST.get('email', '')
- password = request.POST.get('password', '')
- getInfo=users.objects.get(email='example@mail.com')
- user=authenticate(email=username,password=password)
- if user is not None and user.is_active:
- login(request,user)
- return redirect('/')
- args={'form':form} #Fix Indentation
- return render(request,'html/login.html',args) #Fix Indentation
- class users(models.Model):
- id=models.AutoField(primary_key=True)
- name=models.CharField(max_length=100, blank=False)
- email=models.EmailField(max_length=100, blank=False, unique=True)
- password=models.CharField(max_length=500, blank=False)
- created_at=models.DateField(auto_now_add=True)
- updated_at=models.DateField(auto_now_add=True)
- def __str__(self):
- return self.email
- from django.db import models
- from django.contrib.auth.models import User as AuthUser, UserManager
- # Extend the base User model
- class User(AuthUser):
- class Meta:
- db_table = 'user'
- active = models.CharField(max_length=1)
- last_modified = models.DateTimeField("last modified")
- objects = UserManager()
- class CustomAuth(ModelBackend):
- def authenticate(**credentials):
- return super(CustomAuth, self).authenticate(**credentials)
- def authenticate(self, username=None, password=None):
- # Check the username/password and return a User.
- if username != None and password != None:
- # Get the user
- try:
- user = User.objects.get(email=username)
- if user.check_password(password):
- logger.info('User is authenticated, logging user in')
- return user
- except User.DoesNotExist:
- pass
- return None
- def get_user(self, user_id):
- try:
- return User.objects.get(id=user_id)
- except User.DoesNotExist:
- return None
- user = authenticate(username='test@gmail.com', password='testUser')
Add Comment
Please, Sign In to add comment