- Author:
- kcarnold
- Posted:
- June 13, 2008
- Language:
- Python
- Version:
- .96
- Tags:
- middleware serialize json encode encoding serializer
- Score:
- 1 (after 3 ratings)
Makes it really easy to return JSON from your views: just return a dict.
(Also from django-webapp.)
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
- Serialize a model instance by chriswedgwood 1 week, 1 day ago
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 9 months, 1 week ago
- Crispy Form by sourabhsinha396 10 months ago
- ReadOnlySelect by mkoistinen 10 months, 2 weeks ago
- Verify events sent to your webhook endpoints by santos22 11 months, 2 weeks ago
Comments
hm - one problem here is it should also encode lists and tuples, no?
#
Please login first before commenting.