Django Database not retrieving username/password


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

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')

            user = User.objects.get(username=username)
            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')
            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 %}

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

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

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

{% endblock content %}

>Solution :

You have to add name in the form input fields:

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

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

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

Otherwise it will return empty values

Leave a ReplyCancel reply