Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在django中对用户进行身份验证_Python_Django - Fatal编程技术网

Python 无法在django中对用户进行身份验证

Python 无法在django中对用户进行身份验证,python,django,Python,Django,我尝试了一切,但我无法验证用户,它从未登录我,总是给出false,就像它无法读取表一样 user = User.objects.filter(username = username , password = password) 这对于登录非常有效,但身份验证对于专业工作非常重要。 我使用的是mysql数据库,我通过ajax请求传递数据,我将数据毫无问题地传递到函数中。请帮忙 models.py from django.db import models class user(models.M

我尝试了一切,但我无法验证用户,它从未登录我,总是给出false,就像它无法读取表一样

user = User.objects.filter(username = username , password = password)
这对于登录非常有效,但身份验证对于专业工作非常重要。 我使用的是mysql数据库,我通过ajax请求传递数据,我将数据毫无问题地传递到函数中。请帮忙

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函数处理登录,但它不再工作。我告诉你,我没有使用任何django表单或任何东西,我使用html表单并通过ajax请求发送数据,我对注册表单也这样做,但它工作正常,我将数据正确地发送到函数中,但无法进行身份验证。

你需要更改如果用户不是None,则在将语句身份验证为
后使用if语句
尝试使用这种方式登录到您的登录功能

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

如果用户不是None,则需要在身份验证语句之后将if语句更改为

尝试使用这种方式登录到您的登录功能

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