I am creating a project in Django and I have a HelpDesk database that I would like to use in the project. In the database I have the table log_user where the logins information is stored, but I can not find a way to authenticate other than the Django default table.
Views.py
from django.shortcuts import render
from django.http import HttpResponse
from .forms import Person
from django.contrib.auth import authenticate, login
def user_login(request):
if request.method == 'POST':
form = Person(request.POST)
if form.is_valid():
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse('Authenticated sucessfully')
else:
return HttpResponse('Disable account')
else:
return HttpResponse('Invalid Login')
else:
form = Person()
return render(request, 'login2.html', {'form': form})
forms.py
from django import forms
from .models import Loginteste
class Person(forms.ModelForm):
class Meta:
model = Loginteste
fields = ['username', 'password']
models.py
from django.db import models
class Loginteste(models.Model):
username = models.CharField(max_length=50, blank=True, null=True)
password = models.CharField(max_length=50, blank=True, null=True)
class Meta:
managed = True
db_table = 'loginteste'
auth_backend.py
from .models import Loginteste
class MyBackend:
def authenticate(self, request, username=None, password=None):
user = Loginteste.object.get(username=username)
if user.check_password(password):
return (request, user)
I tried with the default table "User" and the login function worked, but I can not do it with an already existing table (log_user in database HelpDesk).
I think I understand your problem now.
As far as I know, Django is really dependant on the
Usermodel; it can be a custom user model, but there has to be some sort ofUsermodel.When you build your own authentication backend, it still has to return
Userinstances (or your customUsermodel instances).For example:
File auth_backends.py
File settings.py
The only relatively simple option you might have is to use the model
Logintesteas your custom user model. Everything else involves overriding a lot of Djangos code to make it work.