Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Login form error message

I've created login form for my Django project, and I have a problem with the error message. This is my function in views.py:

def user_login(request):
if request.method == 'POST':
    username = request.POST.get('username')
    password = request.POST.get('password')

    user = authenticate(username=username, password=password)
    if user:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/friends_plans/users/')
        else:
            return HttpResponse("Your account is disabled")
    else:
        print "Invalid login details: {0}, {1}".format(username, password)
        return HttpResponse("Invalid login details supplied.")
else:
    return render(request, 'friends_plans/index.html', {})

The error message ("Invalid login details supplied") appears on a new blank page. Could you please tell me how to show this message if username or password is incorrect on the same login page (index.html)?

This is my template index.html:

{% load staticfiles %}
<html >
<head >
    <title> Friends' Plans </title>
    <meta charset ="utf -8" />
    <link rel="stylesheet" href="{% static 'css/friends_plans.css' %}">
</head >
<body >
    <div id ="container">
        <div id ="header">
            <ul id ="menu">
                <span><a id="firstbutton" href ="" >Friends' Plans</a> </span>
                <span><a id="helpbutton" href ="" >HELP</a></span>
            </ul>
        </div>
        <div id ="left">
            <form id="login_form" method="post" action="">
                {% csrf_token %}

                Username: <input type ="text" name ="username" value=""  size="50" /> <br />
                Password: <input type ="password" name ="password" value=""  size="50"/> <br />
                <input type ="submit" value="submit" />
            </form>
            {% if user.is_authenticated %}
            <a href="/friends_plans/logout/">Logout</a>
            {% else %}
            <a href="/friends_plans/register/">Register here</a><br />
            {% endif %}
        </div>
        <div id ="right">
            <h1 id="welcome">Welcome to Friends' Plans</h1>
            <img class="cat" src={% static 'images/cat4.jpg' %} />
            <img class="cat" src={% static 'images/cat2.jpg' %} />
            <img class="cat" src={% static 'images/cat3.jpg' %} />
            <img class="cat" src={% static 'images/cat6.jpg' %} />
            <img class="cat" src={% static 'images/cat5.jpg' %} />
            <img class="cat" src={% static 'images/cat1.jpg' %} />
        </div>
        <div id ="footer"> Copyright </div>
    </div>
</body>
</html>

I had an idea to set a variable error=False and to change if for error=Trueif form.is_valid() is False, and to write {% if error %} in the template, but there was a mistake about csrf_token although there is {% csrf_token %} in the template.

like image 601
Anastasia Novikova Avatar asked May 09 '16 20:05

Anastasia Novikova


1 Answers

In views.py:

def login_process(request):
    try:
        user = authenticate(username = request.POST['username'],
            password = request.POST['password'])
    except KeyError:
        return render(request, 'friends_plans/index.html',{
            'login_message' : 'Fill in all fields',}) 
    if user is not None:
        #'User' and 'Pass' right
        if user.is_active:
            login(request, user)
        else:
            return render(request, 'friends_plans/index.html',{
                'login_message' : 'The user has been removed',})
    else:
        return render(request, 'friends_plans/index.html',{
            'login_message' : 'Enter the username and password correctly',})
    return HttpResponseRedirect('/friends_plans/users/')

In index.html:

{% load staticfiles %}
<html >
<head >
    <title> Friends' Plans </title>
    <meta charset ="utf -8" />
    <link rel="stylesheet" href="{% static 'css/friends_plans.css' %}">
</head >
<body >
    <div id ="container">
        <div id ="header">
            <ul id ="menu">
                <span><a id="firstbutton" href ="" >Friends' Plans</a> </span>
                <span><a id="helpbutton" href ="" >HELP</a></span>
            </ul>
        </div>
        <div id ="left">
            <form id="login_form" method="post" action="">
                <div class="add_your_styles">
                    {{ login_message }}
                </div>
                {% csrf_token %}
                Username: <input type ="text" name ="username" value=""  size="50" /> <br />
                Password: <input type ="password" name ="password" value=""  size="50"/> <br />
                <input type ="submit" value="submit" />
            </form>
            {% if user.is_authenticated %}
            <a href="/friends_plans/logout/">Logout</a>
            {% else %}
            <a href="/friends_plans/register/">Register here</a><br />
            {% endif %}
        </div>
        <div id ="right">
            <h1 id="welcome">Welcome to Friends' Plans</h1>
            <img class="cat" src={% static 'images/cat4.jpg' %} />
            <img class="cat" src={% static 'images/cat2.jpg' %} />
            <img class="cat" src={% static 'images/cat3.jpg' %} />
            <img class="cat" src={% static 'images/cat6.jpg' %} />
            <img class="cat" src={% static 'images/cat5.jpg' %} />
            <img class="cat" src={% static 'images/cat1.jpg' %} />
        </div>
        <div id ="footer"> Copyright </div>
    </div>
</body>
</html>

You can place {{ login message }} anywhere. It's just a text string.

like image 118
Henry Iparraguirre Vargas Avatar answered Nov 10 '22 16:11

Henry Iparraguirre Vargas