Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: how to set content-type header to text/xml within a class-based view?

I'm trying to do it this way, but it doesn't work.

class MyView(View):

    def options(self, request, *args, **kwargs):
        """
        Handles responding to requests for the OPTIONS HTTP verb.
        """
        response = http.HttpResponse()
        if self.kwargs.has_key('xml'):
            response['Content-Type'] = 'text/xml; charset=utf-8'
        return response
like image 731
maremare Avatar asked Nov 02 '25 11:11

maremare


1 Answers

You don't need to write additional code. Use TemplateResponseMixin and set content_type attribute to whatever you need:

class MyView(TemplateResponseMixin):
    content_type='application/xml'
    ...
like image 165
Georgy K Avatar answered Nov 04 '25 02:11

Georgy K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!