Login 2.0 Django, authentication

0

I'm trying to make a login page, but I'm having some difficulties. For some reason I can not understand, every time I type the user and the password, whether it is right or not, it gives the answer that the user does not exist.

My data base is SQL Server 2017 .

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'

login2.html

{% extends 'base2.html' %}
{% block content %}
<form action="." method="post" class="form-signin">
    {% csrf_token %}
    <form class="form-signin">
        <h2 class="form-signin-heading">Login</h2>
        <input type="text" name="username" class="form-control" placeholder="Login" required autofocus>
        <input type="password" name="password" class="form-control" placeholder="Password" required>
        <div class="checkbox">
            <label>
                <input type="checkbox" value="remember-me"> Remember me
            </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Entrar</button>
    </form>
</form>
{% endblock %}
    
asked by anonymous 19.06.2018 / 13:18

1 answer

0

Perhaps the problem is that your Loginteste template does not inherit from Django's base classes.

If you are interested in having a template customized for your users, I suggest you refer to the documentation on " Defining a custom user template

Then you could do something like (adapted from the documentation example):

from django.contrib.auth.models import AbstractBaseUser
class Loginteste(AbstractBaseUser):
    identificador = models.CharField(max_length=40, unique=True)

    USERNAME_FIELD = 'identificador'

An alternative, also documented, is with " Extending the Existing User Template

Well, that's what I understood from what your code does. I hope it's useful.

    
22.06.2018 / 10:53