Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:52:52+00:00 2026-05-29T17:52:52+00:00

This is related to this question: Django return json and html depending on client

  • 0

This is related to this question: Django return json and html depending on client python


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 views.py with just one template?

To render the HTML I would use:

return render_to_response('sample/sample.html....')

But how would I do the same for JSON without putting a JSON template? (the content-type should be application/json instead of text/html)

What would determine the JSON and HTML outputs?

So in my views.py:

if something:
    return render_to_response('html_template',.....)
else:
    return HttpReponse(jsondata,mimetype='application/json')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:53:00+00:00Added an answer on May 29, 2026 at 5:53 pm

    I think the issue has gotten confused regarding what you want. I imagine you’re not actually trying to put the HTML in the JSON response, but rather want to alternatively return either HTML or JSON.

    First, you need to understand the core difference between the two. HTML is a presentational format. It deals more with how to display data than the data itself. JSON is the opposite. It’s pure data — basically a JavaScript representation of some Python (in this case) dataset you have. It serves as merely an interchange layer, allowing you to move data from one area of your app (the view) to another area of your app (your JavaScript) which normally don’t have access to each other.

    With that in mind, you don’t "render" JSON, and there’s no templates involved. You merely convert whatever data is in play (most likely pretty much what you’re passing as the context to your template) to JSON. Which can be done via either Django’s JSON library (simplejson), if it’s freeform data, or its serialization framework, if it’s a queryset.

    simplejson

    from django.utils import simplejson
    
    some_data_to_dump = {
       'some_var_1': 'foo',
       'some_var_2': 'bar',
    }
    
    data = simplejson.dumps(some_data_to_dump)
    

    Serialization

    from django.core import serializers
    
    foos = Foo.objects.all()
    
    data = serializers.serialize('json', foos)
    

    Either way, you then pass that data into the response:

    return HttpResponse(data, content_type='application/json')
    

    [Edit] In Django 1.6 and earlier, the code to return response was

    return HttpResponse(data, mimetype='application/json')
    

    [EDIT]: simplejson was remove from django, you can use:

    import json
    
    json.dumps({"foo": "bar"})
    

    Or you can use the django.core.serializers as described above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is more Python related than Django related. I want to test write
This question is related to (but perhaps not quite the same as): Does Django
Important : Please see this very much related question: Return multiple values in C++
This is related to this question . I'm writing a Flex app (a WindowedApplication)
I'm new to python and to django so this question will probably be easy
I am trying to learn Django (and Python) and have created my own app
This question relates to Django Aggregation/Annotation in 1.1. Suppose I have a simple model
This question assumes that the python package I want to install is a django
Although this is django related, it's really just a general, programming efficiency question. I
I'm newcomer in django, and here is question: I have model class: def Client(models.User)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.