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
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 1 week ago
- get_object_or_none by azwdevops 5 months ago
- Mask sensitive data from logger by agusmakmun 6 months, 3 weeks ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
- Serializer factory with Django Rest Framework by julio 2 years, 4 months ago
Comments
Please login first before commenting.