Hi everyone,
I am currently in the middle of creating a django application, with a custom user model and a custom backend , the issue commes when i try to login a user.
the authetification function :
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.views import LoginView
from .models import *
from .forms import *
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import get_user_model
from django.contrib.auth.views import LoginView
User = get_user_model()
@login_required
def create_ticket(request):
if request.method == 'POST':
form = TicketForm(request.POST, request.FILES)
if form.is_valid():
ticket = form.save(commit=False)
ticket.created_by = request.user.personnel
ticket.save()
return redirect('ticket_list')
else:
form = TicketForm()
return render(request, 'myapp/create_ticket.html', {'form': form})
@login_required
def ticket_list(request):
tickets = Ticket.objects.all()
return render(request, 'myapp/ticket_list.html', {'tickets': tickets})
@login_required
def assign_ticket(request, ticket_id):
ticket = Ticket.objects.get(id=ticket_id)
if request.method == 'POST':
technician_id = request.POST.get('technician')
ticket.assigned_to_id = technician_id
ticket.status = 'in_progress'
ticket.save()
return redirect('ticket_list')
technicians = Technician.objects.all()
return render(request, 'myapp/assign_ticket.html', {'ticket': ticket, 'technicians': technicians})
@login_required
def delete_ticket(request, ticket_id):
ticket = Ticket.objects.get(id=ticket_id)
ticket.delete()
class LView(LoginView):
authentication_form = LoginForm
template_name = 'registration/login.html'
def login_form(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
user = authenticate(username=form.cleaned_data["email"], password=form.cleaned_data["password"])
if user is not None:
login(request, user)
else:
return HttpResponse(user)
else:
return HttpResponse(form.is_valid())
else:
return render(request,"registration/login.html",{'form': LoginForm})
always returns none even if the user existes in the database
after a bit of testing , i found that the error commes from the authentification function defined in my custom backend
from django.contrib.auth import get_user_model
User = get_user_model()
class EmailBackend(object):
def authenticate(self,username, password, **kwargs):
try:
user = User.objects.get(email=username)
except User.MultipleObjectsReturned:
user =User.objects.get(email=username).order_by('id').first()
except User.DoesNotExist:
return None
if getattr(user, 'is_active') and user.check_password(password):
return user
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
for some reason always sets the parameters as None , does any one now the reason for this behavior and how to solve it