add JSONRequestMiddleware to your enabled middleware in Django settings. Now, in your view functions, you can call request.json() to get a parsed json body! json is consumed lazily, and cached.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import json from functools import lru_cache, partial from django.http import HttpRequest def _lazy_json(request): return json.loads(request.body) def JSONRequestMiddleware(get_response): def middleware(request: HttpRequest): request.json = lru_cache(partial(_lazy_json, request)) response = get_response(request) return response return middleware |
More like this
- find even number by Rajeev529 1 week, 5 days ago
- Form field with fixed value by roam 1 month ago
- New Snippet! by Antoliny0919 1 month, 1 week ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 3 months, 4 weeks ago
- get_object_or_none by azwdevops 7 months, 3 weeks ago
Comments
Please login first before commenting.