← Back to Chapters

Returning HTML and JSON Responses

? Returning HTML and JSON Responses

? Quick Overview

In Django, views can return HTML responses or JSON data depending on the needs of your application. HTML responses are typically returned when you want to display a page to the user, while JSON responses are used when you're developing APIs or sending data in a machine-readable format.

? Key Concepts

  • Views control what data is returned to the client
  • HTML responses are for web pages
  • JSON responses are for APIs and asynchronous calls
  • Django provides built-in helpers for both

? Syntax / Theory

Django views can return responses using HttpResponse, render(), or JsonResponse. The response type depends on whether you want to return markup or structured data.

? Returning HTML Responses

? Using HttpResponse

? View Code Example
# Returning static HTML directly
from django.http import HttpResponse
def my_view(request):
    return HttpResponse("<h1>Welcome to My Website</h1><p>This is a simple HTML response.</p>")

This approach is useful for very simple responses or quick testing.

? Using render()

? View Code Example
# Rendering a template with dynamic data
from django.shortcuts import render
def home_view(request):
    context = {'message': 'Welcome to my homepage!'}
    return render(request, 'home.html', context)

The render() function combines a template with context data to generate dynamic HTML.

? Returning JSON Responses

? Basic JsonResponse

? View Code Example
# Returning JSON data for an API
from django.http import JsonResponse
def api_view(request):
    data = {'message': 'Hello, World!', 'status': 'success'}
    return JsonResponse(data)

Django automatically serializes Python dictionaries into JSON.

? Custom JSON Data

? View Code Example
# Sending structured product data
from django.http import JsonResponse
def product_api(request):
    product_data = {
        'id': 1,
        'name': 'Laptop',
        'price': 999.99
    }
    return JsonResponse(product_data)

? Live Output / Explanation

HTML Response

Browser renders full web page.

JSON Response

Client receives raw JSON like:

{"message":"Hello, World!","status":"success"}

⚙️ Interactive Example

Simulate a Django View Response: Click a button to see how the client receives the data.

Waiting for request...
Click a button to start the simulation...
? View JavaScript Logic
// Simulated frontend fetch call
fetch("/api/data/")
.then(response => response.json())
.then(data => console.log(data))

This simulates how frontend JavaScript consumes JSON APIs.

? Use Cases

  • HTML responses for dashboards and websites
  • JSON responses for REST APIs
  • AJAX and fetch-based interactions
  • Mobile and third-party integrations

✅ Tips & Best Practices

  • Use render() for templates
  • Use JsonResponse for APIs
  • Keep views focused and simple
  • Validate data before returning JSON

? Try It Yourself

  • Create a view that returns plain HTML
  • Render a template with context data
  • Build a JSON API endpoint
  • Consume JSON using JavaScript fetch