Django -BaseSerializer.is_valid()接受1个位置参数,但给出了2个

iezvtpos  于 2023-01-27  发布在  Go
关注(0)|答案(2)|浏览(127)

这个项目以前是工作的,但后来Heroku取消了免费层,所以我一直试图部署到其他地方,但现在突然我甚至不能在本地创建用户,即使我以前可以...现在当我创建用户时,我得到了标题中提到的错误。

序列化程序文件夹****公用.py文件

from xml.dom import ValidationErr
from rest_framework import serializers
from django.contrib.auth import get_user_model, password_validation
from django.contrib.auth.hashers import make_password
User = get_user_model()

class UserSerializer(serializers.ModelSerializer):

  password = serializers.CharField(write_only=True)
  confirm_password = serializers.CharField(write_only=True)

  def validate(self, data):
    password = data.pop('password')
    confirm_password = data.pop('confirm_password')

    if password != confirm_password:
    raise ValidationErr({ 'confirm_password': 'Does not match the password'})

    password_validation.validate_password(password)

    data['password'] = make_password(password)

    return data
    
  class Meta:
    model = User
    fields = ('id', 'username', 'email', 'password', 'confirm_password')

视图.py文件

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.exceptions import PermissionDenied
from django.contrib.auth import get_user_model
import jwt
User = get_user_model()
from datetime import datetime, timedelta
from jwt_auth.serializers.common import UserSerializer
from django.conf import settings

class RegisterView(APIView):

  def post (self, request):
    create_user = UserSerializer(data=request.data)
    try:
    create_user.is_valid(True)
    create_user.save()
    return Response(create_user.data, status=status.HTTP_201_CREATED)
    except Exception as e:
    print(str(e))
    return Response(e.__dict__ if e.__dict__ else str(e),        status=status.HTTP_422_UNPROCESSABLE_ENTITY)

class LoginView(APIView):
  def post(self, request):
    password = request.data.get('password')
    username = request.data.get('username')
    try:
    user_login = User.objects.get(username=username)
    except User.DoesNotExist:
    raise PermissionDenied('Credentials are incorrect!')
    if not user_login.check_password(password):
    raise PermissionDenied('Credentials are incorrect!')

    dt = datetime.now() + timedelta(days=7)

    token = jwt.encode(
    {
    'sub': user_login.id,
    'exp': int(dt.strftime('%s'))
    },
    settings.SECRET_KEY,
    'HS256'
    )
    print('TOKEN ----->', token)
    return Response({ 'token': token, 'message': f'Welcome back {user_login.username}' })
idfiyjo8

idfiyjo81#

剩余框架分支“3.9.x”(github)

def is_valid(self, raise_exception=False):
     ...

rest框架分支“master”(github)

def is_valid(self, *, raise_exception=False):
     ...

所以现在“掌握”:is_valid()只接受关键字参数“raise_exception=True”:

class RegisterView(APIView):

  def post (self, request):
    create_user = UserSerializer(data=request.data)
    try:
    create_user.is_valid(raise_exception=True)
    ....
r7knjye2

r7knjye22#

把它改成这样:

def post(self, request):
    serializer = UserSerializer(data=request.data)
    if serializer.is_valid():
        user = serializer.save()
        return Response(user.data, status=status.HTTP_201_CREATED)

    the rest of your code...

相关问题