login page not working

35 views
Skip to first unread message

KUMBHAGIRI SIVAKRISHNA

unread,
Jul 3, 2019, 7:02:52 AM7/3/19
to Django users
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%}


Joe Reitman

unread,
Jul 3, 2019, 10:13:54 AM7/3/19
to Django users
Try debugging your view with a print statement. Print the username and password to see if those values are getting passed in.

Aldian Fazrihady

unread,
Jul 3, 2019, 10:39:57 AM7/3/19
to django...@googlegroups.com
Redirection should only be done on successful login. 
Invalid login should directly render the form plus error message. 

Regards, 

Aldian Fazrihady

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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.

KUMBHAGIRI SIVAKRISHNA

unread,
Jul 3, 2019, 12:04:39 PM7/3/19
to django...@googlegroups.com
Even I was entered wrong username and  wrong password  ,but it directs to same login with empty spaces. 

Aldian Fazrihady

unread,
Jul 3, 2019, 9:48:29 PM7/3/19
to django...@googlegroups.com
Your code shows you that it redirects on invalid login instead of immediately rendering the same form plus error message. 

Reply all
Reply to author
Forward
0 new messages