[Answer]-Django Braces' "JSONResponseMixin" shows weird behavior

1👍

content_type = u"application/json"

This line is actually used by DetailView. My best guess is that this messed up your template’s rendering; either by using "application/json" or by not supplying a charset.

I would leave the default content_type alone (use content_type = None) and override get_content_type to supply the content type you need when using Ajax.

To fully incorporate JSON into your view, you should probably overwrite render_to_response on your PostDetail view:

class PostDetail(JSONResponseMixin, DetailView):
    model = Post
    template_name = 'post_detail_page.html'
    content_type = None

    def render_to_response(self, **kwargs):
        if self.request.is_ajax():
            # Don't really know if objects will take a list, a queryset, any iterable or even a single object
            return self.render_json_object_response(objects=[self.object]):
        else:
            return super(PostDetail, self).render_to_response(**kwargs)

    def get_content_type(self):
        if self.request.is_ajax():
            return u'application/json'
        else:
            return None
👤knbk

Leave a comment