Login 2.0 Django, authentication

Asked

Viewed 682 times

0

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

My databank is the 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']

py.models

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 %}
  • why do you use two forms nested?

1 answer

0

Maybe the problem is that your model Loginteste does not inherit from the basic classes of Django.

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

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 model" - also in Portuguese.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.