TypeError: login()接受1个位置参数,但给出了2个

时间:2021-12-31 23:20:05

i have written a login view using buid in auth ,django auth.login() gives above error my code with error code o 500

我在auth、django .login()中编写了一个使用buid的登录视图,它给出了上面的错误代码,错误代码为o 500

from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,logout,login


@api_view(['POST'])
def register(request):
    user=User.objects.create_user(username=request.POST['username'],email=request.POST['email'],password=request.POST['password'])
    return Response({'ok':'True'},status=status.HTTP_201_CREATED)

@api_view(['POST'])
def login(request):
    user=authenticate(
        username=request.POST['username'],
        password=request.POST['password']
    )
    if user is not None:
        login(request,user)
        return Response({'ok':'True'},status=status.HTTP_200_OK)
    else:
        return Response({'ok':'False'},status=status.HTTP_401_UNAUTHORIZED)

1 个解决方案

#1


30  

Your view has the same name as the auth login function, so it is hiding it. Change the view name, or import the function under a different name eg from django.contrib.auth import login as auth_login.

您的视图具有与auth登录函数相同的名称,因此它正在隐藏它。更改视图名称,或以不同的名称(如来自django.com)导入该函数。auth导入登录为auth_login。

#1


30  

Your view has the same name as the auth login function, so it is hiding it. Change the view name, or import the function under a different name eg from django.contrib.auth import login as auth_login.

您的视图具有与auth登录函数相同的名称,因此它正在隐藏它。更改视图名称,或以不同的名称(如来自django.com)导入该函数。auth导入登录为auth_login。