Django Database not retrieving username/password

Advertisements

IMage can’t be posted some reason but it returns both errors:

User does not exist

Username OR Password not Valid

It seems Django is unable to access the database of users as well as passwords.
The password consists of numbers, letters, upper/lowercase and a special character $
Below is my login function for LoginPage under views.py

from django.shortcuts import render, redirect
from django.contrib import messages
from django.db.models import Q
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from .models import Room, Topic
from .forms import RoomForm


# Create your views here.

#
# rooms = [
    #  {'id':1, 'name':'Lets learn Python!'},
    # {'id':2, 'name':'Design with me!'},
    # {'id':3, 'name':'Front End Devs!'},
#]


def loginPage(request):

    if request.method == 'POST':
        password = request.POST.get('password')
        username = request.POST.get('username')

        try:
            user = User.objects.get(username=username)
        except:
            messages.error(request, 'User does not exist')
            user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('home')
        else:
            messages.error(request, 'Username OR Password not Valid')

    context = {}
    return render(request, 'base/login_register.html', context)

The Login_Register.html page code –

{% extends 'main.html' %}

{% block content %}

<div>
    <form method="POST" action="">
        {% csrf_token %}

        <label>Username:</label>
        <input type="text" value="username" placeholder='Enter Username..' />

        <label>Password:</label>
        <input type="password" value="password" placeholder='Enter Password..' />
        
        <input type="submit" value="login" />
    </form>
</div>

{% endblock content %}

>Solution :

You have to add name in the form input fields:

<form method="POST" action="">
    {% csrf_token %}

    <label>Username:</label>
    <input type="text" name="username" placeholder='Enter Username..' />

    <label>Password:</label>
    <input type="password" name="password" placeholder='Enter Password..' />
        
    <input type="submit" value="login" />
</form>

Otherwise it will return empty values

Leave a ReplyCancel reply