Search code examples
djangodjango-modelsdjango-viewsdjango-formsdjango-templates

login page is not redirecting to index page even after entering correcting login information


I am trying to buid a django application with login form, my code looks like

login.html

<form class="container my-5" action="/" method="post">
        {% csrf_token %}
        <div class="row mb-3">
          <label for="name" class="col-sm-2 col-form-label">name</label>
          <div class="col-sm-10">
            <input type="text" class="form-control" id="name" name="username">
          </div>
        </div>
        <div class="row mb-3">
          <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
          <div class="col-sm-10">
            <input type="password" class="form-control" id="password" name="password">
          </div>
        </div>      
        
        <button type="submit" class="btn btn-primary">Sign in</button>
      </form>

Views.py

from django.shortcuts import render,redirect
from django.contrib.auth import authenticate
from django.contrib.auth import logout,login
from django.contrib.auth.models import User

def index(request):
    if request.user.is_anonymous:
        return redirect("/login")        
    return render(request,'index.html')
def loginuser(request):
        if request.method=="POST":
         username=request.POST.get('username')
         password=request.POST.get('password')
         print(username,password)
         user = authenticate(username=username, password=password)
         if user is not None:
            login(request,user)  
            return redirect("/")
         else:
            
            return render(request,'login.html')
    # No backend authenticated the credentials   
        return render(request,'login.html')
def logoutuser(request):
    logout(request)
    return redirect('/login')  

urls.py


from django.contrib import admin
from django.urls import path,include
from home import views
urlpatterns = [   `your text`
    path('', views.index, name="index"),
    path('login', views.loginuser, name="login"),
    path('logout', views.logoutuser, name="logout"),
]

I tried to print username and password but it is not coming in the terminal Can anyone please help me with that?


Solution

  • your action is being done directly without reaching the view, your login.html should be like this:

    <form class="container my-5"  method="post">
        {% csrf_token %}
        <div class="row mb-3">
            <label for="name" class="col-sm-2 col-form-label">name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" name="username">
            </div>
        </div>
        <div class="row mb-3">
            <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" id="password" name="password">
            </div>
        </div>
    
        <button type="submit" class="btn btn-primary">Sign in</button>
    </form>
    

    what has technically happened is that your action in your HTML has overridden your post request.