我刚接触Django,我的项目是REST。当我使用postman检查URL时,出现了以下错误:
"detail": "请求中的媒体类型不受支持 "multipart/form-data; boundary=----WebKitFormBoundaryAU1ShXHTHrfcQr61"。
而在使用curl时,出现了以下错误:
{"detail":"JSON解析错误 - 预期值:第1行第1列(char 0)"}
这是我用于示例URL的视图:
@api_view(["POST"])
def register(request):
user_data = request.data
if user_data:
serializer = UserSerializers(data=user_data)
if serializer.is_valid():
registerInfo = {
'name': serializer.validated_data['name'],
'degImage': serializer.validated_data['degImage'],
}
serializer.save(registerInfo)
return Response({
"code": 200,
"status": "successful",
"message": "code was sent try to validate code"
})
else:
return Response({
"status": "error",
"error": "Bad request"
})
else:
return Response({
"status": "error",
"error": "Empty request"
})
这是序列化器类:
class UserSerializers(serializers.Serializer):
name = serializers.CharField(required=True)
degImage = serializers.ImageField(required=True)
def save(self, validated_data):
user = Users(
name=validated_data['name'],
degImage=validated_data['degImage']
)
user.save()
return user
以及模型:
class Users(models.Model):
name = models.CharField(max_length=20, null=True)
degImage = models.ImageField(null=False, blank=False, default='detImage')