Performance: Request parsing and response rendering
This concept affects server response time and how quickly the browser receives and renders content.
Jump into concepts and practice - no test required
from django.shortcuts import render def view(request): context = request.GET.dict() return render(request, 'template.html', context)
def view(request): data = request.GET context = {} for key in data: context[key] = data[key] html = render_to_string('template.html', context) return HttpResponse(html)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual request parsing + string rendering | N/A (server-side) | N/A | N/A | [X] Bad |
| Django render shortcut with context dict | N/A (server-side) | N/A | N/A | [OK] Good |
JsonResponse in Django?JsonResponse is a Django class that formats Python data as JSON and sends it as an HTTP response.json.loads() or similar, not JsonResponse.request?json.loads().request.body contains raw bytes, so decode if needed, then parse with json.loads().from django.http import JsonResponse
import json
def my_view(request):
data = json.loads(request.body)
result = {"message": f"Hello, {data['name']}!"}
return JsonResponse(result){"name": "Alice"}json.loads(request.body) to get a Python dict with key 'name' and value 'Alice'.def my_view(request):
data = json.loads(request.POST)
return JsonResponse({"status": "ok"})request.POST is a QueryDict, not a JSON string, so passing it to json.loads() causes an error.json.loads(request.body) instead, since request.body contains raw JSON bytes.values, sums them, and returns the sum as JSON. Which code correctly implements this?json.loads(request.body) to get Python dict from JSON input.JsonResponse.