Hello everyone,
I met an issue to display a variable in my template in order to print a message if the authentification is not right. I don’t know what I’m doing wrong, my webpage doesn’t display the message ‘Invalide credentials’. I’ve no warning from the terminal. Please find my code:
- In views.py:
def login_view(request):
if request.method == "POST":
username = request.POST["username"]
password = request.POST["password"]
user = authenticate(request, username = username, password = password)
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
render(request, "authentification/login.html", {
'message': 'Invalide credentials'
})
return render(request, "authentification/login.html")
- In authentification/login.html
{% extends "authentification/base.html" %}
{% block body %}
{% if message %}
<div> {{ message }}</div>
{% endif %}
<form action="{% url 'login' %}" method="post" autocomplete="off">
{% csrf_token %}
<input type = "text" name="username" placeholder="Username">
<input type = "password" name="password" placeholder="Password">
<input type = "submit" value="login">
</form>
{% endblock %}
- In authentification/base.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Authentification</title>
</head>
<body>
<h1>Authentification:</h1>
{% block body %}
{% endblock %}
</body>
</html>
Any help will be helpful