Issue display variables in template

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 :pray:

You’re rendering a response within the else clause with the message in the context, but then your code is rendering the template without the context.
Your return statement is returning the rendered version without the context.

Of course, I forgot the return… I’m feel so stupid :confused:

Thank you a lot @KenWhitesell and have a nice day.

SOLVED.