Returning JSON with JsonResponse
📖 Scenario: You are building a simple Django web app that needs to send data as JSON to the browser. This is common when making APIs or dynamic web pages that use JavaScript to fetch data.
🎯 Goal: Create a Django view that returns a JSON response using JsonResponse with a dictionary of user information.
📋 What You'll Learn
Create a dictionary called
user_data with keys name, age, and city and their exact values.Create a variable called
status_code and set it to 200.Write a Django view function called
user_info that returns a JsonResponse with user_data and status=status_code.Add the necessary import for
JsonResponse from django.http.💡 Why This Matters
🌍 Real World
APIs often send data as JSON to web browsers or other apps. Django's JsonResponse makes it easy to send JSON data from your server.
💼 Career
Knowing how to return JSON responses is essential for backend web developers building APIs or dynamic web applications.
Progress0 / 4 steps