Django:如何在基于类的视图中将内容类型标头设置为 text/xml?

mar*_*are 3 django http-headers mime-types django-class-based-views

我正在尝试这样做,但它不起作用。

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
Run Code Online (Sandbox Code Playgroud)

Geo*_*y K 7

您不需要编写额外的代码。使用TemplateResponseMixin并将content_type属性设置为您需要的任何内容:

class MyView(TemplateResponseMixin):
    content_type='application/xml'
    ...
Run Code Online (Sandbox Code Playgroud)