title: How do I return JSON without using a template in Django?
body: <p>This is related to this question: <a href="https://stackoverflow.com/questions/9260249/django-return-json-and-html-depending-on-client-python">Django return json and html depending on client python</a></p>
  
  <hr>
  
  <p>I have a command line Python API for a Django app. When I access the app through the API it should return JSON and with a browser it should return HTML. I can use different URLs to access the different versions but how do I render the HTML template and JSON in the <strong>views.py</strong> with just one template?</p>
  
  <p>To render the HTML I would use:</p>
  
  <pre><code>return render_to_response('sample/sample.html....')
  </code></pre>
  
  <p>But how would I do the same for JSON without putting a JSON template? (the <code>content-type</code> should be <code>application/json</code> instead of <code>text/html</code>)</p>
  
  <p>What would determine the JSON and HTML outputs?</p>
  
  <p>So in my <strong>views.py</strong>:</p>
  
  <pre><code>if something:
      return render_to_response('html_template',.....)
  else:
      return HttpReponse(jsondata,mimetype='application/json')
  </code></pre>