title: Subclass HttpResponse to get JSON output in Django
body: <p>I have a number of views in a Django app that all return something like this:</p>
  
  <pre><code>return HttpResponse(jsonpickle.encode(data, unpicklable=False), 'application/json')
  </code></pre>
  
  <p>This works well, but I'd like to create an abstraction over the JSON encoding and creating the response object, so that I can do something like</p>
  
  <pre><code>return JsonResponse(data)
  </code></pre>
  
  <p>and the class <code>JsonResponse</code> does all the heavy lifting for me.</p>
  
  <p>I tried something like this:</p>
  
  <pre><code>class JsonResponse(HttpResponse):
      def __init__(self, obj):
          super(HttpResponse, self).__init__(jsonpickle.encode(obj, unpicklable=False), 'application/json')
  </code></pre>
  
  <p>but when I have that, I get the standard <code>A server error occurred.  Please contact the administrator.</code> when I view the web page. I've also tried with <code>self</code> as the first argument to the inner call to <code>__init__</code> as well as with the arguments named (<code>content</code> and <code>content_type</code> respectively) and with and without a named <code>status=200</code>. Neither of these changes seem to change anything.</p>
  
  <p>The terminal output from the dev server is a little more descriptive, but not by much:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
      self.result = application(self.environ, self.start_response)
    File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/core/handlers/wsgi.py", line 267, in __call__
      start_response(force_str(status), response_headers)
    File "/usr/lib/python2.7/wsgiref/handlers.py", line 175, in start_response
      assert int(status[:3]),"Status message must begin w/3-digit code"
  ValueError: invalid literal for int() with base 10: 'app'
  [02/Jun/2013 00:51:06] "GET / HTTP/1.1" 500 59
  </code></pre>
  
  <p>I know I could just create a method that returns the <code>HttpResponse</code> instead, like so:</p>
  
  <pre><code>def json(obj):
      return HttpResponse(...)
  </code></pre>
  
  <p>but I'd like to learn a way to do this the way I originally imagined it if it's possible - if nothing else then for my learning (and it also seems to align with the design of Django, which I like).</p>
  
  <p>Is there a way to get a subclass like <code>JsonResponse</code> above to work? If so, what am I doing wrong?</p>