title: Using Django's JSONResponseMixin to respond to AJAX requests
body: <p>I'm working with Django &amp; AJAX. Basically, I simply want the javascript (vote.js) to POST some data 
  to the Django View, and in turn, the View to respond with a JSON data to the html, so that my javascript's callback function can use that response from the server.</p>
  
  <p>So here is my code:</p>
  
  <p><strong>vote.js</strong></p>
  
  <pre><code>$(document).on('click', 'a.upvote', function() {
      .....
  
      var xhr = {
          'id': id,
          'upvote': upvote,
      };
  
      $.post(location.href, xhr, function(data) {
          question.find('.rating').html(data.rating)
      });
  
      return false;
  });
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>//I copied this JSONResponseMixin directly from official Django doc
  class JSONResponseMixin(object):
      def render_to_response(self, context):
          "Returns a JSON response containing 'context' as payload"
          return self.get_json_response(self.convert_context_to_json(context))
  
      def get_json_response(self, content, **httpresponse_kwargs):
          "Construct an `HttpResponse` object."
          return http.HttpResponse(content,
                                   content_type='application/json',
                                   **httpresponse_kwargs)
  
      def convert_context_to_json(self, context):
          "Convert the context dictionary into a JSON object"
          # Note: This is *EXTREMELY* naive; in reality, you'll need
          # to do much more complex handling to ensure that arbitrary
          # objects -- such as Django model instances or querysets
          # -- can be serialized as JSON.
          return json.dumps(context)
  
  
  class MyListView(JSONResponseMixin, TemplateResponseMixin, DetailView):
      def post(self, request, *args, **kwargs):
          id = request.POST.get('id')
          .....
  
          data = {'rating': question.rating}
  
          return render_to_response(data)
  
  
      def render_to_response(self, context):
          if self.request.is_ajax():
              return JSONResponseMixin.render_to_response(self, context)
          else:
              return TemplateResponseMixin.render_to_response(self, context)
  </code></pre>
  
  <p>However, doing this and clicking my "vote" button in html that triggers the javascript POST gives me a TemplateDoesNotExist error:</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>.....
  File "/Library/Python/2.7/site-packages/django/template/loader.py", line 139, in find_template                                                      
      raise TemplateDoesNotExist(name)                                               
  TemplateDoesNotExist: {'rating': 1}
  </code></pre>
  
  <p>Well it looks like my last 5 lines of views.py are working properly. 
  Any idea??? :(((</p>
  
  <p>Thanks!!!!</p>