简体   繁体   中英

Not able to authenticate user in django

i tried everything but i cant authenticate user it never logs me in always gives false like its not able to read the table

user = User.objects.filter(username = username , password = password)

this works perfectly for login but authentication is important for professional work. i am using mysql database and i am passing the data through ajax request and i am getting the data to the function without any problem. please help..

models.py

from django.db import models


class user(models.Model):
    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=100)
    fullname = models.CharField(max_length=100)
    email = models.EmailField(max_length=100)
    phone = models.CharField(max_length=50)
    password = models.CharField(max_length=100)
    ipaddress = models.CharField(max_length=200)
    class Meta:
        app_label = 'Play'
        db_table = 'user'

Views.py

from django.shortcuts import render
from django.contrib.auth import authenticate
from django.contrib.auth import authenticate, login, logout
from Accounts.EmailAuthentication import EmailBackend
from Play.models import user
from django.contrib.auth.models import User

from django.shortcuts import redirect
from django.shortcuts import HttpResponse
from django.contrib.auth.hashers import make_password

def JoinRequest(request):

    if request.method == 'POST':
        fullname = request.POST['fullname']
        email = request.POST['email']
        username = request.POST['username']
        phone = request.POST['phone']
        password = request.POST['password']
        cpassword = request.POST['cpassword']

        #encpass = make_password(password)

        def get_client_ip(request):
            x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
            if x_forwarded_for:
                ip = x_forwarded_for.split(',')[-1].strip()
            else:
                ip = request.META.get('REMOTE_ADDR')
            return ip

        if password != cpassword:
            return HttpResponse('Password Not Matching To Confirm Password Value..')

        else:
            senddata = user(username=username,fullname=fullname, email=email, phone=phone, password=password , ipaddress=get_client_ip(request))
            senddata.save(commit=False)


            return HttpResponse('')



def Join_View(request):
    return render(request, 'Join.html', {})

def login_view(request):

    return render(request, 'Login.html', {})


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

        #userr = user.objects.filter(username=username, password=password)
        userr = authenticate(request, username=username, password=password)
        if userr:
            login(request , userr)
            return HttpResponse('DONE')
        else:
            return HttpResponse("NONE")

LoginREQUEST function handles the login but it never works again i am telling i am not using any django forms or anything i am using html form and sending the data by ajax request and i am doing same with signup form but it works prefectly and i am getting the data to the functions properly but not able to authenticate.

You need to change your if statement after authenticate statement as if user is not None Try this way to your login function

from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login

def login(request):
    if request.user.is_authenticated:
        return redirect("/service-page.html")

    if request.method == "POST":
        username = request.POST["username"]
        password = request.POST["password"]

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

        if user is not None:
            login(request, user)
            return redirect("/service-page.html")
        else:
            messages.error(request,"Invaild Credentials, Please try again")
            return render(request,"login.html")
    else:
        return HttpResponse("Only POST Methods are allowed baby")
    return HttpResponse("Wrong password")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM