Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
DRF Imagefield upload responds with html template instead of JSON

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;
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda