I'm working with Django Rest Framework 3.5.4 Django 1.9 python 3.5 and Angular 1.4.5. I'm having an issue where I can upload/post to an image endpoint but the response I get is the DRF template instead of the JSON that I was expecting.
models.py
class BeforeImage(models.Model):
job = models.ForeignKey(Job, related_name='before_images')
before_image = models.ImageField(upload_to='images/', default='images/no-image.jpg')
def __str__(self):
return self.pk
class Meta:
db_table = 'before_images'
serializers.py
class BeforeImageSerializer(ModelSerializer):
before_image = ImageField(
max_length=None, allow_empty_file=False, use_url=True)
class Meta:
model = BeforeImage
fields = ('id', 'job', 'before_image')
views.py
class BeforeImageViewSet(ModelViewSet):
queryset = BeforeImage.objects.all()
serializer_class = BeforeImageSerializer
Payload
------WebKitFormBoundarybB5wAXF1Q6ZaG9pB Content-Disposition: form-data; name="job" 10 ------WebKitFormBoundarybB5wAXF1Q6ZaG9pB Content-Disposition: form-data; name="before_image"; filename="Screen Shot 2017-04-10 at 11.44.24 PM.png" Content-Type: image/png ------WebKitFormBoundarybB5wAXF1Q6ZaG9pB--
content-type
Content-Type:multipart/form-data;
That's content negotiation and the preferred renderer when submitting forms is HTML because that's consistent with browsers.
If you want a JSON response, you should make sure your request set the headers so it gets JSON. In other words, you need to set the Accept headers to application/json