Redirection should only be done on successful login. Invalid login should directly render the form plus error message.
Regards, Aldian Fazrihady On Wed, 3 Jul 2019, 18:03 KUMBHAGIRI SIVAKRISHNA, <[email protected]> wrote: > I entered username and password ,and click submit ,then it directs same > login page with empty username and password columns, logic is correct in > views.py > Please help me ,give any suggestions to solve this issue > views.py: > ========== > from django.shortcuts import render ,redirect > from django.contrib import messages > from django.contrib.auth.models import User,auth > from django.contrib.auth import authenticate, login,logout > def Login(request): > if request.method=='POST': > username = request.POST['username'] > password = request.POST['password'] > > user = auth.authenticate(username=username,password=password) > > if user is not None: > auth.login(request, user) > return redirect('/') > else: > messages.info(request,'invalid credentials') > return redirect('Login') > else: > return render(request,'login.html') > > > > > login.html: > ============ > <!DOCTYPE html> > <html lang="en"> > <head> > <meta charset="UTF-8"> > <meta name="viewport" content="width=device-width, initial-scale=1.0"> > <meta http-equiv="X-UA-Compatible" content="ie=edge"> > <title>Login</title> > </head> > <body> > <form action="Login" action="POST"> > {% csrf_token %} > <input type="text" name="username" placeholder="USERNAME"><br> > <input type="password" name="password" placeholder="PASSWORD"><br> > <input typev="submit"> > </form> > > </body> > <div> > {% for message in messages %} > <h4>{{message}}</h4> > {% endfor %} > </div> > </html> > > > > home.html: > =============== > > {% if request.user.is_authenticated %} > <li>Hello,{{user.first_name}}</li> > <li><a href="accounts/Logout">Logout</a></li> > {%else%} > <li><a href="accounts/register">Register</a></li> > <li><a href="accounts/Login">Login</a></li> > {%endif%} > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/c2984d46-acfb-49a5-a93a-062797e98a23%40googlegroups.com > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAN7EoAZPeeRZnXGsPryNQnv-iqe7nH6YLVFMNHsgiZDixP0qSA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

