I have a project made with function based views. I need to show an error
message when the user enters the wrong password, but in every forum I've seen so far there is a suggestion to use a Form
. There is no way to show an error message with the project done this way?
view.py:
def login(request):
if request.method == "GET":
return render(request, 'users/login.html')
else:
Email = request.POST.get('Email')
Senha = request.POST.get('Senha')
user = authenticate(username=Email, password=Senha)
if user:
loginDjango(request, user)
return render(request, 'convite/cadastro_convite.html')
else:
return redirect('login')
html:
<div >
<h2 >Faça seu login!</h2>
<form action="{% url 'login'%}" method="POST">
{% csrf_token %}
<div >
<input type="text" id="log-email" aria-describedby="emailHelp" name="Email" placeholder="Email">
</div>
<div >
<input type="password" id="log-senha" name="Senha" placeholder="Senha">
</div>
<button href="/Exemplo" type="submit" >
<i ></i> Acessar
</button>
</form>
</div>
model:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
NomeUsuario = models.TextField(blank=True)
Endereco = models.TextField(blank=True)
Celular = models.TextField(blank=True)
Cidade = models.TextField(blank=True)
Estado = models.TextField(blank=True)
Cep = models.TextField(blank=True)
Bairro = models.TextField(blank=True)
CodePudding user response:
While it seems using a django
form
is more efficient, yes there is a way you could do it from what you have by just adding a few lines of code.
But here's my first recommendation... Rename your defined method called login
to loginDjango
. By doing that you could use the django
login
method without having any potential issues.
You can use the code below to solve your problem:
from django.contrib.auth import login, get_user_model
from django.shortcuts import get_object_or_404
def loginDjango(request):
context = {}
if request.method == "GET":
return render(request, 'users/login.html', context)
else:
found = False
Email = request.POST.get('Email')
Senha = request.POST.get('Senha')
# Try retrieving the user object from the get_user_model method
user = get_object_or_404(get_user_model(), email=Email) # Assuming the email field is unique.
# If a user object was returned then you can use the check_password method on the user object.
if user:
if user.check_password(Senha): # If the passwords matched
found = True
else:
context['password_error'] = 'You have entered an incorrect password!'
else:
context['email_error'] = "Sorry, this email address doesn't exist!"
# If the email exists and the password match then found is true...
# Can use the authenticate within this if statement
if found:
# From the user object, using user.username -> username=user.username
user = authenticate(username=user.username, password=Senha)
# This if statement is not really necessary again but you can have it as a fail-safe
if user:
# Use the django login method to log the user in.
login(request, user)
return render(request, 'convite/cadastro_convite.html')
return render(request, 'users/login.html', context)
Then within your template, you can check for the errors you sent via the context dictionary.
<div >
<input type="text" id="log-email" aria-describedby="emailHelp" name="Email" placeholder="Email">
{% if email_error %}<span>{{ email_error }}</span>{% endif %}
</div>
<div >
<input type="password" id="log-senha" name="Senha" placeholder="Senha">
{% if password_error %}<span>{{ password_error }}</span>{% endif %}
</div>
But I do recommend that you give doc a read for the djano forms
as well though.
CodePudding user response:
The easier way to do this is using Django messages Framework
from django.contrib import messages
messages.info(request, '#message you want to show in the template')
You can use For Loop or If statement
Like:
{% for message in messages %}
<p>{{message}}></p>
{% endfor %}