Return JSON with JsonResponse

Build small JSON endpoints without DRF.


# models.py

from django.db import models

class Stat(models.Model):
    key = models.CharField(max_length=50, unique=True)
    value = models.IntegerField(default=0)
  

# admin.py

from django.contrib import admin
from .models import Stat

@admin.register(Stat)
class StatAdmin(admin.ModelAdmin):
    list_display = ("key","value")
  

# views.py
from django.http import JsonResponse
def stats_view(request):
    data = {"active": 12, "visits": 345}
    return JsonResponse(data)

# urls.py
path("stats.json", stats_view)
  
Explanation:
  • JsonResponse serializes dicts automatically to JSON.
  • Set safe=False if you return a list instead of a dict.
  • Category Views (FBV & CBV)
  • Total Views 1214
  • Last Modified 21 December, 2025
  • Tags #json #response #fbv #cbv
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.