我是Django的新人。 我的项目是在DjangoRestFramework
这个项目有一个用户:
models.py:
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(upload_to='Images/degrees/', null=False, blank=False,default='Images/degrees/None/no-img.jpg')
natImage = models.ImageField(upload_to='Images/nationalCards/', null=False, blank=False,default='Images/nationalCards/None/no-img.jpg')
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)
last_seen = models.DateTimeField(default=django.utils.timezone.now)
points = models.IntegerField(null=False, default=0)
scorers = models.IntegerField(null=False, default=0)
并在views.py中我创建了一个注册函数,但对于degImage和natImage存在问题。
views.py
@api_view(["POST"])
@parser_classes((MultiPartParser, JSONParser))
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=***&Password=***&Source=***&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"
})
serializers.py
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=False)
natImage = serializers.ImageField(required=False)
code = serializers.CharField(required=False)
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'],
code=validated_data['code']
)
user.save()
return user
在Postman中,当我选择一个文件时,它可以正常工作
但是在开发Android和iOS时,他们无法在json中使用其他字段发布图像
我想知道我的编码是否存在任何问题导致此问题或我们可以采取的任何措施来解决此问题
如果有任何事情要迅速做到这一点请告诉我
我在服务器上运行我的项目,你可以使用下面的ip检查它:
答案 0 :(得分:1)
我们无法发送带有标题" Content-Type:application / json"的文件类型。
请确保您的请求标题应设置为"内容类型:multipart / form-data" 。
当您使用解析器MultiPartParser, JSONParser
时,它将解析文件数据以及json数据。