I am new to Django Authentication system and I am unable to find the correct debugging method.
I want to create a function to handle login requests and I have done the necessary steps to do the same.
created a login url path in main project URLS.py file.
path('members/', include('django.contrib.auth.urls')), path('members/', include('members.urls')),created a login url in members app to point to a function created in views.py
urlpatterns = [ path('login/', views.login_user, name='login'),]defined what to do when user comes to specific url
def login_user(request): if request.method == 'POST': print('-'*100) username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) messages.success(request, ("You are now logged in")) return redirect('index') else: messages.success(request, ("Invalid credentials")) return redirect('login') return render(request, 'registration/Login.html')I have created a Login Page in templates folder.
{% extends 'Base.html'%} {% block title %} Login to the Blog Page {% endblock %} {% block content%} <h1>Members Login</h1> <div class="form-group"> <form method="POST" action=""> {% csrf_token %} <div class="mb-3"> <label for="exampleInputEmail1" class="form-label">User Name</label> <input type="text" class="form-control" name = "username"> <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div> </div> <div class="mb-3"> <label for="exampleInputPassword1" class="form-label">Password</label> <input type="password" class="form-control" name="password"> </div> <button type="submit" class="btn btn-primary">Login</button> </form> <br> </div> {% endblock %}
Now when I get to the /members/login after submitting the user details the print statement in post method is not printed in the console. So I am suspecting that the post request is not being redirected to the login_user function. Can anyone help me out to identify why?