django framework separates the front and rear ends, and the back end does not report errors. When registering, errors pop up
. Request URL: http://api.meiduo.site:8000/users/
Request Method: POST
Status Code: 400 Bad Request
Remote Address: 127.0.0.1
Referrer Policy: no-referrer-when-downgrade
my 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
my 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):
-sharp
pwd1=attrs.get("password")
pwd2=attrs.get("password2")
if pwd1 != pwd2:
raise serializers.ValidationError("")
-sharp
-sharp
request_code=attrs.get("sms_code")
-sharp redis
mobile=attrs.get("mobile")
redis_cli=get_redis_connection("smscode")
redis_code=redis_cli.get("sms_"+mobile)
-sharp
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")
-sharp User.objects.create(username=username,password=password)
user=User.objects.create_user(username,password=password,mobile=mobile)
return user
help me, God!