简体   繁体   English

无法验证 django 中的用户

[英]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.我正在使用 mysql 数据库,我正在通过 ajax 请求传递数据,并且我正在毫无问题地将数据发送到 function。 please help..请帮忙..

models.py模型.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视图.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. 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数据正确传递给功能但无法进行身份验证。

You need to change your if statement after authenticate statement as if user is not None Try this way to your login function您需要在身份验证语句之后更改您的 if 语句,就if user is not None尝试这种方式来登录 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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