gpt4 book ai didi

python - Rest 框架中的 Django ImageField

转载 作者:太空宇宙 更新时间:2023-11-04 04:50:44 24 4
gpt4 key购买 nike

我是 Django 的新手。我的项目在 DjangoRestFramework 中

这个项目有一个用户:

模型.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"
})

序列化器.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中用其他字段发布图片

我想知道我的编码中是否有任何问题导致了这个问题,或者我们可以做些什么来解决这个问题

如果在swift中有任何事情可以做到这一点,请告诉我

我在服务器上运行我的项目,您可以使用下面的 ip 检查它:

http://176.31.82.35:3030/moshaver/register

最佳答案

我们无法发送 header 为“Content-Type:application/json”的文件类型。请确保您的请求 header 应设置为 "Content-Type: multipart/form-data"。当您使用解析器 MultiPartParser, JSONParser 时,它将解析文件数据和 json 数据。

关于python - Rest 框架中的 Django ImageField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48396819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com