Issue
my login page:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Page Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
<script src='main.js'></script>
</head>
<body>
<form action="login" media="post">
{% csrf_token %}
<input type="text" name="username" placeholder="username"><be>
<input type="password" name="password" placeholder="password"><be>
<input type="Submit">
</form>
</body>
</html>
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
# Create your views here.
def homepage(request):
return render(request, 'homepage.html')
def login(request):
if request.method== 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
return redirect("/")
else:
messages.success(request,'password not matching')
return redirect('home')
else:
return render(request,'login.html')
def registration(request):
if request.method == 'POST':
first_name = request.POST['first_name']
last_name = request.POST['last_name']
username = request.POST['username']
password = request.POST['password']
confirmpassword = request.POST['confirmpassword']
email = request.POST['email']
if password==confirmpassword:
if User.objects.filter(username=username).exists():
messages.info(request, 'Username Taken')
return redirect('/registration')
elif User.objects.filter(email=email).exists():
messages.info(request, 'Email Taken')
return redirect('/registration')
else:
user = User.objects.create_user(username=username,
password=password, email=email, first_name=first_name, last_name=last_name)
user.save();
print('user successfully created')
return redirect('login')
else:
messages.info(request,'password not matching')
return redirect('/registration')
return redirect('/')
else:
return render(request, 'registration.html')
my urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('',views.homepage, name='homepage'),
path('login',views.login, name='login'),
path('registration',views.registration, name='registration'),
]
every single time this login page is redirecting to same login page, whether I enter wrong credentials or the right one, I think its not even verifying the credentials that whether they are right or wrong. please help me out. please help me out. please help me out. please help me out. please help me out.
Solution
You checked if there is a user with as username 'username'
and as password 'password'
, you do not use the user
and password
variables, but strings that contain username
and password
as content. You thus should use variables:
# not 'username' and 'password'
user = authenticate(username=username, password=password)
Answered By - Willem Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.