1 2 3 4 5 6 7 8 9 10 | from snippet 801 import JsonResponse
class JsonEncodingMiddleware(object):
'''If a view returns a dict, encode it as JSON.
This should be the last middleware in MIDDLEWARE_CLASSES.'''
def process_response(self, request, response):
if isinstance(response, dict):
return JsonResponse(response)
else:
return response
|
More like this
- Ajax error handling by kcarnold 4 years, 11 months ago
- Ajax API class by kcarnold 4 years, 11 months ago
- Deep json serialization by Alexey Boriskin 3 years, 10 months ago
- Admob Ad Insertion Snippet by johnboxall 4 years, 10 months ago
- Format transition middleware by limodou 6 years, 2 months ago
Comments
hm - one problem here is it should also encode lists and tuples, no?
#