Issue
When trying to create a simple web page with login and sign up i ran into the issue of django not recognzing the created user. In views.py i created a simple message that displays "Incorrect password or email" if user does not exist. I created a user and made sure that the password was correct when i inputed it into the login form however it still gave me the message of "Incorrect password or email". Instead of redirecting to home like a created user should it errored and i could not figure out why it is not accepting the password/email in the form.
views.py
from django.shortcuts import redirect, render
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth.models import User
def home(request):
return render(request, 'home.html')
def signup(request):
if request.method == "POST":
email = request.POST['email']
password = request.POST['password']
myuser = User(email=email)
myuser.set_password(password)
myuser.save()
messages.success(request, "Your account has been successfully created ")
return redirect('signin')
return render(request, 'signup.html')
def signin(request):
if request.method == "POST":
email = request.POST['email']
password = request.POST['password']
user = authenticate(request, email = email, password = password)
if user is not None:
login(request, user)
email = user.email
messages.success(request, "Successfully Logged In")
return redirect ('home')
else:
messages.info(request, "Email or Password is Incorrect")
return render(request, 'signin.html')
return render(request, 'signin.html')
def signout(request):
pass
urls.py
from django.urls import path
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('', views.home, name = "home"),
path('signin', views.signin, name = "signin"),
path('signup', views.signup, name = "signup"),
]
urlpatterns += staticfiles_urlpatterns()
signup.html
<h1>Sign up</h1>
<form action = "/signup" method = "POST">
{% csrf_token %}
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name = "email" aria-describedby="emailHelp" placeholder = "example@example.com">
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name = "password">
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
signin.html (login page)
<h1>Login</h1>
{%if messages%}
{%for message in messages%}
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Hey,</strong> {{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
{% endif %}
<form action = "/signin" method = "POST">
{% csrf_token %}
<div class="row mb-3">
<label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name = "email">
</div>
</div>
<div class="row mb-3">
<label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id = "password" name = "password">
</div>
<div class="col-sm-10">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
Solution
What is the status of your user. An inactive user is one that has its is_active field set to False. The default backend ModelBackend and RemoteUserBackend prohibits the inactive users from authenticating. You didn't specify user status. So please check the user status also.
Answered By - user8193706
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.