title: Authentication login with legacy database in Django
body: <p>I am creating a project in Django and I have a <code>HelpDesk</code> database that I would like to use in the project. In the database I have the table <code>log_user</code> where the logins information is stored, but I can not find a way to authenticate other than the Django default table.</p>
  
  <p><strong>Views.py</strong></p>
  
  <pre><code>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})
  </code></pre>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>from django import forms
  from .models import Loginteste
  
  class Person(forms.ModelForm):
      class Meta:
          model = Loginteste
          fields = ['username', 'password']
  </code></pre>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>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'
  </code></pre>
  
  <p><strong>auth_backend.py</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I tried with the default table "User" and the login function worked, but I can not do it with an already existing table (<code>log_user</code> in database <code>HelpDesk</code>).</p>