Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Content-Disposition header using Django Rest Framework

I am serving an image using the Django REST framework. Unfortunately it downloads instead of displays. I guess I have to set the header Content-Disposition = 'inline'. How do I do this in the View or the Renderer?

class ImageRenderer(renderers.BaseRenderer):
    media_type = 'image/*'
    format = '*'
    charset = None
    render_style = 'binary'

    def render(self, data, media_type=None, renderer_context=None):
        return data

class ImageView(APIView):
    renderer_classes = (ImageRenderer, )

    def get(self, request, format=None):
        image=MyImage.objects.get(id=1)
        image_file = image.thumbnail_png.file
        return Response(image)
like image 681
Ryan Avatar asked Dec 21 '18 16:12

Ryan


1 Answers

According to this page in the Django docs, you can set the Content-Disposition header in this way:

response = Response(my_data, content_type='image/jpeg')
response['Content-Disposition'] = 'attachment; filename="foo.jpeg"'
like image 176
Ken4scholars Avatar answered Nov 15 '22 17:11

Ken4scholars