猿问

Status Code: 400 Bad Request

django 框架做前后端分离,后端不报错,做注册时,弹出错误

Request URL: http://api.meiduo.site:8000/users/
Request Method: POST
Status Code: 400 Bad Request
Remote Address: 127.0.0.1:8000
Referrer Policy: no-referrer-when-downgrade

我的users/views.py

from rest_framework.views import APIView
from .models import User
from rest_framework.response import Response
from rest_framework.generics import CreateAPIView
from .serializers import CreateUserSerializer
class UsernamecountView(APIView):
    def get(self,request,username):
        count=User.objects.filter(username=username).count()

        return Response({
            'username':username,
            'count':count
        })     
class MobilecountView(APIView):
    def get(self,request,mobile):
        count=User.objects.filter(mobile=mobile).count()

        return Response({
            'mobile': mobile,
            'count': count
        })
class UserView(CreateAPIView):
    serializer_class = CreateUserSerializer

我的serializers.py

from rest_framework import serializers
from .models import User
import re
from django_redis import get_redis_connection
class CreateUserSerializer(serializers.Serializer):
    id=serializers.IntegerField(read_only=True)
    username=serializers.CharField(
        max_length=20,
        min_length=5,
        error_messages={
            'min_length':'用户名为5-20个字符',
            'max_length':'用户名为5-20个字符'
        }
    )
    password=serializers.CharField(
        write_only=True,
        max_length=20,
        min_length=8,
        error_messages={
            'min_length': '密码为8-20个字符',
            'max_length': '密码为8-20个字符'
        }
    )
    password2=serializers.CharField(write_only=True)
    sms_code=serializers.IntegerField(write_only=True)
    mobile=serializers.CharField(max_length=11)
    allow=serializers.BooleanField(write_only=True)
    def validate_username(self,value):
        if User.objects.filter(username=value).count:
            raise serializers.ValidationError('用户名存在')
        return value
    def validate(self, attrs):
        # 验证密码
        pwd1=attrs.get('password')
        pwd2=attrs.get('password2')
        if pwd1 != pwd2:
            raise serializers.ValidationError('两次密码不一致')
        # 验证  验证码
        # 1.获取请求报文中的验证码
        request_code=attrs.get('sms_code')
        # 2.读取redis中的验证码,先获取手机号
        mobile=attrs.get('mobile')
        redis_cli=get_redis_connection('smscode')
        redis_code=redis_cli.get('sms_'+mobile)
        # 3.验证码的有效时间为300秒,可能超时
        if redis_code is None:
            raise serializers.ValidationError('验证码失效')
        if int(redis_code) != request_code:
            raise serializers.ValidationError('验证码错误')
        return attrs
    def validate_mobile(self,value):
        if User.objects.filter(mobile=value).count:
            raise serializers.ValidationError('手机号已注册')
        if not re.match(r'^1[3-9]\d{9}$',value):
            raise serializers.ValidationError('手机号格式错误')
        return value
    def validate_allow(self,value):
        if value==False:
            raise serializers.ValidationError('请同意协议')
        return value
    def create(self,validate_data):
        username=validate_data.get('username')
        mobile=validate_data.get('mobile')
        password=validate_data.get('password')
        # User.objects.create(username=username,password=password)
        user=User.objects.create_user(username,password=password,mobile=mobile)
        return user

大神帮一下!!!

侃侃尔雅
浏览 3574回答 3
3回答

红颜莎娜

后端接收参数用的是formdate,看看前端向后端传递参数时的content-type是不是json,换成formdata方式

慕婉清6462132

400 一般是客户端请求错误, 具体根据实际请求参数与响应判断

元芳怎么了

你的请求有问题,是不是参数漏传了?或者对方要POST你是GET?可能情况很多,一句话就是你的请求和服务端预期的不一样
随时随地看视频慕课网APP

相关分类

Python
我要回答