JSON responses and fetch

A Django view can return JSON with JsonResponse, which serves as a lightweight API consumed by JavaScript. On the browser side, fetch() sends the request and reads the response with response.json(). For POST requests, you must send the CSRF token.

Example

return JsonResponse({'id': post.id, 'title': post.title})

Related concepts

The whole glossary