Django views.py function doesn't get called

I want the function login_user() to be called when the user clicks the Log In Button. But the function never gets called. I think I have setup all the URLConfs correctly. Can you spot my mistake?

views.py

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

# Create your views here.


def login_user(request):
   if request.method == "POST":  # if user goes to webpage and fills out form, do something
       email = request.POST['email']
       password = request.POST['password']
       user = authenticate(request, email=email, password=password)
       if user is not None:
           login(request, user)
           return redirect('home')  # Redirect to a success page.
       else:
           messages.success(request, "There was an Error Logging In, please try again.")
           return redirect('login')

   else:
       return render(request, 'registration/login.html', {})

urls.py (main config)

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('loginForm.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
]

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('login', views.login_user, name='login'),
]

login.html

<h2>Log In</h2>
    <form method="POST">
        {% csrf_token %}
        <div class="mb-3">
            <label for="email" class="form-label">Email address</label>
            <input type="email" class="form-control" name="email" aria-describedby="emailHelp">
        </div>
        <div class="mb-3">
            <label for="exampleInputPassword1" class="form-label">Password</label>
            <input type="password" class="form-control" name="password">
        </div>
    <input type="submit" value="Log In" class="btn btn-secondary">
    </form>

Note: Even if I print something in the first line of login_user(), I can see that the function never gets called.

from my side setup working fine.

i taken username instead of email in my case.

===== views.py =====

def login_user(request):
   if request.method == "POST":  # if user goes to webpage and fills out form, do something
       uname = request.POST['uname']
       password = request.POST['password']
       user = authenticate(request, username=uname, password=password)
       if user is not None:
           login(request, user)
           return redirect('home')  # Redirect to a success page.
       else:
           messages.success(request, "There was an Error Logging In, please try again.")
           return redirect('login')

   else:
       return render(request, 'index.html', {})

==== projects/urls.py ====

from django.contrib import admin
from django.urls import path,include


urlpatterns = [
    path("", include("myapp.urls")),
    path('admin/', admin.site.urls),

]

==== myapp/urls.py =====

from django.urls import path
from .views import *
 
urlpatterns = [
 
    path('', login_user,name='home'),
    path('login', login_user, name='login'),
 
]

===== index.html =====

<div class="container my-5">
  <div class="row">
    <div class="col-5">
      <h2>Log In {{request.user}}</h2>
      <form method="POST">
        {% csrf_token %}
        <div class="mb-3">
          <label for="email" class="form-label">Email address</label>
          <input type="text" class="form-control" name="uname" aria-describedby="emailHelp">
        </div>
        <div class="mb-3">
          <label for="exampleInputPassword1" class="form-label">Password</label>
          <input type="password" class="form-control" name="password">
        </div>
        <input type="submit" value="Log In" class="btn btn-secondary">
      </form>
    </div>
  </div>

======= Output =======

enter image description here

Back to Top