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 > <form method="POST" action=""> {% csrf_token %} <div > <label for="exampleInputEmail1" >User Name</label> <input type="text" name = "username"> <div id="emailHelp" >We'll never share your email with anyone else.</div> </div> <div > <label for="exampleInputPassword1" >Password</label> <input type="password" name="password"> </div> <button type="submit" >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?
CodePudding user response:
Djnago will fire the view that it first finds for the path members/login
, and that is the login view of the django.contrib.auth
module. You can swap the order of the views to visit the login
view of your view:
urlpatterns = [
path('members/', include('members.urls')), #