Django login authentication redirect to userlogin

54 Views Asked by At

i having problem to login user . if i enterd a valid login username and password it keep redirecting me to the login page instead of index.html page for authenticated user

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.urls import reverse
 
from django.db import models
from django.contrib import messages
from django.contrib.auth.models import User
from finance.models import User

from django.contrib.auth import authenticate, login


# Create your views here.


def userlogin(request):
    
    return render(request,'userlogin.html')

def Checklogin(request):
    if request.method == 'POST':
        usernmame = request.POST['username']
        password  = request.POST['password']

        user = authenticate(username=usernmame,password=password)
        if user is not None:
                login(request, user)
                return redirect(request,'index.html')
        else:
            return render(request,'userlogin.html')
            
1

There are 1 best solutions below

1
Ankit Singh On

There is a typo in your redirect function. Instead of passing request as the first argument, you should directly provide the URL you want to redirect to.

if user is not None:
    login(request, user)
    return redirect('index')  # Provide the URL name or the actual URL to redirect to
else:
    return render(request, 'userlogin.html', {'error_message': 'Invalid login credentials'})