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?– JonatasCD