Home > Mobile >  Django is not recongizing a users password when login is attempted
Django is not recongizing a users password when login is attempted

Time:01-05

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 >
      <label for="exampleInputEmail1" >Email address</label>
      <input type="email"  id="email" name = "email" aria-describedby="emailHelp" placeholder = "[email protected]">
      
    <div >
      <label for="exampleInputPassword1" >Password</label>
      <input type="password"  id="password" name = "password">
    </div>
    <button type="submit" >Sign up</button>
  </form>
  
  

signin.html (login page)

<h1>Login</h1>
{%if messages%}
  {%for message in messages%}
<div  role="alert">
    <strong>Hey,</strong> {{ message }}
    <button type="button"  data-bs-dismiss="alert" aria-label="Close"></button>
</div>
  {% endfor %}
{% endif %}

<form action = "/signin" method = "POST"> 
  {% csrf_token %}
  <div >
    <label for="inputEmail3" >Email</label>
    <div >
      <input type="email"  id="email" name = "email">
    </div>
  </div>
  <div >
    <label for="inputPassword3" >Password</label>
    <div >
      <input type="password"  id = "password" name = "password">
    </div>
    <div >
  </div>
  <button type="submit" >Sign in</button>
</form>
  

CodePudding user response:

You can improve this part as following:

myuser = User(email=email)
myuser.set_password(password)
myuser.save()

To set password you have to use set_password function.

CodePudding user response:

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.

  •  Tags:  
  • Related