我是Django的新人,我的项目是REST 当我使用邮递员检查网址时,它会犯这个错误:
" detail":"不支持的媒体类型\" multipart / form-data;边界= ---- WebKitFormBoundaryAU1ShXHTHrfcQr61 \"在请求中。"
使用curl时发生了这个错误:
{" detail":" JSON解析错误 - 期望值:第1行第1列(字符0)"}
这是我对示例网址的看法:
@api_view(["POST"])
def register(request):
user_data = request.data
if user_data:
serializer = UserSerializers(data=user_data)
if serializer.is_valid():
phone_number = serializer.validated_data["phone"]
try:
found_user = Users.objects.get(phone=phone_number)
except Users.DoesNotExist:
found_user = None
if found_user:
return Response({
"code": 211,
"status": "successfull",
"message": "user already exists, try to login"
})
else:
destination = serializer.validated_data['phone']
message = str(random.randint(1000, 9999))
url = "https://panel.asanak.ir/webservice/v1rest/sendsms/?Username=0216463&Password=123456&Source=02100064636463&Destination={}&message={}"
url = url.format(destination, message)
r = requests.get(url)
r.json()
registerInfo = {
'name': serializer.validated_data['name'],
'lastName': serializer.validated_data['lastName'],
'phone': serializer.validated_data['phone'],
'natNum': serializer.validated_data['natNum'],
'password': serializer.validated_data['password'],
'degImage': serializer.validated_data['degImage'],
'natImage': serializer.validated_data['natImage'],
'sex': "",
'province': "",
'city': "",
'job': "",
'code': message
}
serializer.save(registerInfo)
return Response({
"code": 200,
"status": "successfull",
"message": "code was sent try to validate code"
})
return Response({
"status": "successfull",
"error": "user created :)"
})
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)
lastName = serializers.CharField(required=True)
phone = serializers.CharField(required=True)
natNum = serializers.IntegerField(required=True)
password = serializers.CharField(required=True)
degImage = serializers.ImageField(required=True)
natImage = serializers.ImageField(required=True)
def save(self, validated_data):
user = Users(
name=validated_data['name'],
lastName=validated_data['lastName'],
phone=validated_data['phone'],
natNum=validated_data['natNum'],
password=validated_data['password'],
degImage=validated_data['degImage'],
natImage=validated_data['natImage']
)
user.save()
return user
和模型:
class Users(models.Model):
name = models.CharField(max_length=20, null=True)
lastName = models.CharField(max_length=50, null=True)
phone = models.IntegerField(unique=True, null=False, default='phone')
password = models.CharField(max_length=25, null=True)
natNum = models.IntegerField(unique=True, null=True)
degImage = models.ImageField(null=False, blank=False, default='detImage')
natImage = models.ImageField(null=False, blank=False, default='natImage')
sex = models.CharField(null=True, max_length=1)
province = models.CharField(null=True, max_length=20)
city = models.CharField(null=True, max_length=40)
job = models.CharField(null=True, max_length=20)
code = models.CharField(max_length=4, null=True)
答案 0 :(得分:2)
在邮递员设置中,正文为原始,然后从下拉菜单中选择 JSON 并发送请求。
更改Django rest框架中的解析器
settings.py
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
)
}
答案 1 :(得分:1)
您可能正在使用您的方法尚未准备好使用的MIME类型发送您的请求。
在这里查看媒体类型:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
在不查看您的请求的情况下不能说太多,但我希望它有所帮助。