'Django Authentication method is not working
I am trying to use the default Django from django.contrib.auth authenticate() method to authenticate if the user exists. I am doing this right after the user registers. The user registers and their username, email, and password is inputted into the database then they are taken to the login page but it is not working.
Here is my views.py
from django.shortcuts import render,redirect
from .models import Student
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
# Create your views here.
def index(request):
if request.method == "POST":
fname = request.POST.get('fname')
lname = request.POST.get('lname')
pnumber = request.POST.get('pnumber')
email = request.POST.get('email')
password = request.POST.get('password')
student = Student(fname = fname, lname = lname, pnumber = pnumber, email = email,
password = password)
student.save()
user = User.objects.create_user(fname, email, password)
user.save()
return render(request, ('SignUpPage.html'))
def loginUser(request):
if request.method == "POST":
email = request.POST.get('email')
password = request.POST.get('password')
user = authenticate(email=email, password=password)
if user is not None:
login(request, user)
return redirect('mainPage')
else:
return render(request, ('LoginPage.html'))
return render(request, ('LoginPage.html'))
def mainPage(request):
return render(request, ('MainPage.html'))
Here is my urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('login', views.loginUser, name="login"),
path('mainPage',views.mainPage, name='mainPage')
]
Here is my html code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login</title>
<link
href="https://fonts.googleapis.com/css2?family=Raleway:ital,wght@0,300;1,300&display=swap"
rel="stylesheet"
/>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
list-style: none;
font-family: "Raleway", sans-serif;
font-weight: bolder;
}
body {
background-image: url(clean-medical-background_53876-97927.webp);
background-repeat: no-repeat;
background-size: 1920px 975px;
}
.wrapper {
min-height: 90vh;
display: flex;
justify-content: right;
align-items: center;
margin-right: 500px;
}
.login_form {
background: white;
padding: 25px;
border-radius: 50px;
width: 400px;
}
.login_form .title {
text-align: center;
font-size: 30px;
text-transform: uppercase;
color: #77d5cc;
letter-spacing: 4px;
font-weight: 900;
}
.form_wrap {
margin-top: 35px;
}
.form_wrap .input_wrap {
margin-bottom: 20px;
}
.form_wrap .input_wrap:last-child {
margin-bottom: 10;
}
.form_wrap .input_wrap label {
display: block;
margin-bottom: 3px;
}
.form_wrap input[type="text"] {
width: 100%;
border-radius: 3px;
border: 1.3px solid #9597a6;
padding: 10px;
outline: none;
}
.form_wrap input[type="password"] {
width: 100%;
border-radius: 3px;
border: 1.3px solid #9597a6;
padding: 10px;
outline: none;
}
.form_wrap input[type="text"]:focus {
border-color: #77d5cc;
}
.form_wrap input[type="password"]:focus {
border-color: #77d5cc;
}
.form_wrap .login_btn {
text-align: center;
width: 100%;
background: #77d5cc;
padding: 10px;
border: 0;
color: white;
font-size: 17px;
border-radius: 3px;
text-transform: uppercase;
letter-spacing: 2px;
cursor: pointer;
}
.form_wrap .login_btn:hover {
background: #bde9e3;
}
.form_wrap p {
font-family: "Raleway", sans-serif;
text-align: center;
}
.form_wrap a {
display: block;
text-align: center;
width: 30%;
margin-left: 122px;
background: #77d5cc;
padding: 10px;
border: 0;
color: white;
font-size: 15px;
border-radius: 3px;
text-transform: uppercase;
letter-spacing: 2px;
cursor: pointer;
}
.form_wrap a:hover{
background-color: #bde9e3;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="login_form">
<div class="title">
<label>Login</label>
</div>
<form action="/login" method="POST">
{% csrf_token %}
<div class="form_wrap">
<div class="input_wrap">
<label for="email">Email Address</label>
<input type="text" name = "email" id="email" />
</div>
<div class="input_wrap">
<label for="password">Password</label>
<input type="password" name = "password" id="password" />
</div>
<div class="input_wrap">
<input type="submit" value="Login" class="login_btn" />
</div>
<div class="input_wrap">
<p>Don't Have Account! Sign Up Here</p>
</div>
<div class="input_wrap">
<a href="/" target="_blank">Sign Up</a>
</div>
</div>
</form>
</div>
</div>
Solution 1:[1]
You are trying to authenticate with email, basic Django's auth is with username. If you want to use email, you have to tell Django that. Add to your User model this:
class User(...):
...
USERNAME_FIELD = "email"
...
Also you said that User registers username but in view you uses only fname and lname. Please be specific and read what you write, otherwise we cannot help you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | NixonSparrow |
