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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 5 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
- Stuff by NixonDash 1 year, 8 months ago
Comments
Please login first before commenting.