Authenticate() is not validate data properly django

When I try to click on login button it always execute the invalid credentials instead of redirect to the index page.. What I did is that in database create table name signup and wants to validate all the data from that table.. Here signup_data function is works well but in login_data cannot authenticate the user.

from django.db import models

class signup(models.Model):
    username = models.CharField(max_length=10)
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    email = models.EmailField()
    password = models.CharField(max_length=10)

from django.forms import ModelForm
from . models import signup
from django import forms

class signupform(ModelForm):
    username= forms.CharField(max_length=10,widget=forms.TextInput(attrs={'class':'form-control'}))
    first_name = forms.CharField(max_length=20, widget=forms.TextInput(attrs={'class': 'form-control'}))
    last_name = forms.CharField(max_length=20,widget=forms.TextInput(attrs={'class': 'form-control'}))
    email =  forms.EmailField(max_length=20,widget=forms.EmailInput(attrs={'class': 'form-control'}))
    password = forms.CharField(max_length=10,widget=forms.PasswordInput(attrs={'class':'form-control'}))

    class Meta:
        model = signup
        fields = '__all__'

from django.shortcuts import render,redirect
from . forms import  signupform
from . models import signup
from django.contrib import messages
from django.contrib.auth  import login,authenticate

def index(response):
    return render(response,'login_module/index.html')

def signup_data(response):
    if response.method == 'POST':
        form = signupform(response.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            if signup.objects.filter(username=username).exists():
                # messages.add_message(response,messages.WARNING,'Username is already taken')
                messages.error(response,'Username is already taken')
                return redirect('signup')

            elif signup.objects.filter(email=email).exists():
                messages.error(response,'Email is already taken')
                # messages.add_message(response,messages.WARNING,'Email is already taken')
                return redirect('signup')
                register_instance = signup(username=username, first_name=first_name, last_name=last_name, email=email, password=password)
                messages.success(response,'Registered Successfull')
                return redirect('signup')

        form = signupform()
    return render(response,'login_module/signup.html',{'form':form, 'message': messages})

def login_data(request):
    form = signupform(request.POST or None)
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)

        if user is not None:
            return redirect('index')
            messages.error(request,'Invalid Credentials')   
            return redirect('login')
        return render(request,'login_module/login.html',{'form':form, 'message': messages})


{% extends 'login_module/base.html' %}
{% block content %}
<div class="container">
    <div class="row">
        <div class="col-md-12 text-center mt-50">
            <h1>Login Page</h1>
    <div class="row">
        <div class="col-md-4 offset-md-4 ">
            <form method="post" action="{% url 'login' %}">
                {% csrf_token %}
                <div class="form-group rounded-top">
                    {{ form.username.label_tag }} {{ form.username }}
                <div class="form-group rounded-top">
                    {{ form.password.label_tag }} {{ form.password }}
                <div class="text-center">
                    <button type="submit" class="btn btn-primary">Login</button>

      <div class="text-center mt-50">
            {% if messages %}
                {% for message in messages %}
                <div class="alert alert-danger" role="alert">
                {{ message }}
                {% endfor %}
            {% endif %}      
{% endblock %}

Login Page

As far as I can tell you are creating a signup object when signing up, but when trying to login with authenticate django is looking for the generic User model. You have to create a User in the database rather than a signup object to use authenticate.

Back to Top