I'm new at Django. my project is in DjangoRestFramework
This project has a user:
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)
and in views.py I made a function for registeration but for degImage and natImage there is a problem.
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
In Postman when i choose a file it's OK and works fine
But in developing android and iOS they can't post a image in json with other fields
and i want to know is there any problem in my coding that makes this problems or any thing which we can do to solve this
if there is any thing in swift to do this please tell me
i run my project on a server and you can check it to with the ip below:
http://176.31.82.35:3030/moshaver/register
we cannot send a filetype with header "Content-Type: application/json".
Please make sure your request header should be set to "Content-Type: multipart/form-data".
As you are using parsers MultiPartParser, JSONParser
it will parse the file data as well as json data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With