Why is my Django login not getting authenticated?

984 Views Asked by At

I do know that this is a repeated question. I did refer those answers but still I couldn't get my issue fixed. Kindly do help. Registration works just fine. The registered users gets added to the DB fine. Logout works fine. The issue is with login. Whenever I try logging in , I keep getting the response "Invalid login details".

views.py

from django.shortcuts import render, redirect
from . forms import *
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required


# USER LOGIN

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 is not None:
            if user.is_active:
                login(request,user)
                return redirect('home')

            else:
                return HttpResponse("Account not active")

        else:
            return HttpResponse("Invalid login details")    #whenever I try to login, I always gets this response.

    else:
        return render(request, 'basic_app/login.html')




#USER LOGOUT

@login_required
def user_logout(request):
    logout(request)
    return redirect('login')

urls.py(application).

from django.urls import path
from . import views


urlpatterns = [
    path('',views.index, name='home'),
    path('register/',views.register, name='register'),
    path('login/',views.user_login, name='login'),
    path('logout/',views.user_logout, name='logout'),
]

login.html

{% extends 'basic_app/base.html' %}

{% block body_block %}
<div class='jumbotron'>
    <h2>User Login</h2>

    <form action="{% url 'login' %}" method="post">
        {% csrf_token %}
        <label for="username">Username:</label>
        <input id="username" type="text" placeholder="Enter Username">

        <label for="password">Password</label>
        <input id="password" type="password" placeholder="Enter Password">

        <input type="submit" value="Login">

    </form>
</div>

{% endblock %}

settings.py

LOGIN_URL = '/basic_app/login/'
2

There are 2 best solutions below

2
On

Try this:-

Make a copy of your Project in another folder. AND delete the current database and 
create a new Database as usual like :- IN Terminal - Create Superuser command is :- 
python manage.py createsuperuser.
0
On

In your views.py, in the user_login function you are checking if the user.is_active.

Instead try checking if the user is authenticated in the if statement.

Because I think that the user would probably be inactive before being logged in or authenticated.

For example, this is from the documentation:

    if request.user.is_authenticated:
    # Do something for authenticated users.
    ...
else:
    # Do something for anonymous users.
    ...

Check the documentation here

Edit

Sorry, I was lookin at the wrong if block.

You don't seem to have assigned anything to the user variable. That maybe why the user is returned as none.

Also you can try:

user = request.user