簡體   English   中英

無法驗證 django 中的用戶

[英]Not able to authenticate user in django

我嘗試了所有方法,但我無法驗證用戶身份,它從不讓我登錄,總是給出錯誤,就像它無法讀取表格一樣

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

這非常適合登錄,但身份驗證對於專業工作很重要。 我正在使用 mysql 數據庫,我正在通過 ajax 請求傳遞數據,並且我正在毫無問題地將數據發送到 function。 請幫忙..

模型.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'

視圖.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數據正確傳遞給功能但無法進行身份驗證。

您需要在身份驗證語句之后更改您的 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