Skip to content

Instantly share code, notes, and snippets.

@yeazin
Last active September 26, 2021 06:30
Show Gist options
  • Save yeazin/aec6e37b95feca8a43c9b52158b10707 to your computer and use it in GitHub Desktop.
Save yeazin/aec6e37b95feca8a43c9b52158b10707 to your computer and use it in GitHub Desktop.
Django Multiple Username Field
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{% if request.user.is_authenticated %}
You are logged In
<a href="{% url 'logout' %}">Logout</a>
{% else %}
<h1>Login here</h1>
<form action="{% url 'login' %}" method="POST">
{% csrf_token %}
<input type="text" name="detect" id="" placeholder="Type Username , MemberId">
<input type="password" name="password" id="" placeholder="type password">
<input type="submit" name="" id="">
</form>
{% endif %}
</body>
</html>
# Django Multiple username Field
'''
We can use default user model for this example. If you want to create the Custom user model You can do the same.
'''
# importing modules
from django.db import models
from django.contrib.auth.models import User
# Creating Profile Model
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,null=True )
name = models.CharField(max_length=300, blank=True, null=True)
memberID = models.CharField(max_length=300, blank=True, null=True,unique=True)
phoneNum = models.CharField(max_length = 20, blank = True, null= True, unique=True)
def __str__(self):
return self.name
'''
Creating urls
'''
# importing initials modules
from django.urls import path
from .views import LoginFront,LogoutView
urlpatterns = [
path('login/user/',LoginFront.as_view(),name='login'),
path('logout/',LogoutView.as_view(),name='logout')
]
# Importing initials modules
from django.views import View
from django.contrib.auth import authenticate, login,logout
from django.db.models import Q
from django.shortcuts import redirect, render
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .models import profile
# Logics
class LoginFront(View):
def get(self,request,*args,**kwargs):
return render(request,'login.html')
def post(self,request,*args,**kwargs):
# Getting Input from Frontend
detect = request.POST.get('detect')
password = request.POST.get('password')
# Matching the Queries from Database
match = User.objects.filter(
Q(username=detect)| ## matching userame or
Q(email=detect) | ## matching the email or
Q(profile__memberID=detect)| ## matching the memberId under profile models
Q(profile__phoneNum=detect) ## matching the phone number under profile models
## You can add more with the same pattern
).first()
if match:
print(match.username) # print the username in terminal if necessary
user = authenticate(username=match.username,password=password)
if user is not None:
login(request,user)
return redirect('login')
else:
return HttpResponse('sorry didnot match')
# Logout View
class LogoutView(View):
@method_decorator(login_required(login_url='login'))
def dispatch(self,request,*args,**kwargs):
return super().dispatch(request,*args,**kwargs)
def get(self,request,*args,**kwargs):
logout(request)
return redirect('login')
@yeazin
Copy link
Author

yeazin commented Sep 24, 2021

Do you have any Query?
join me then

Linkedin : yeazin
facebook : yeazin
email : naz.yeasin@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment