Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading