Created
October 2, 2017 19:47
-
-
Save emmettna/02100a022e865eaa47170dc24e45ce9c to your computer and use it in GitHub Desktop.
Django Python3 AsciiFalseJsonResponse Decorator and fuction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from django.http import HttpResponse | |
import json | |
""" | |
Either use it as a decorator or as a function | |
But When using as a decorator, Make sure you return dictionary object. | |
Both HttpResponse and JsonResponse will cause an Error. | |
""" | |
class AsciiFalseJsonResponse: | |
def __init__(self, f): | |
self.func = f | |
def __call__(self, *args, **kwargs): | |
return HttpResponse(json.dumps(self.func(*args, **kwargs), ensure_ascii=False), content_type='application/json') | |
# AsciiFalseJsonResponse 's function version | |
def ascii_false_json_response_function_version(func): | |
@wraps(func) # just adding additional information. Nothing really important for function. | |
def do_the_job(*args, **kwargs): | |
return HttpResponse(json.dumps(func(*args, **kwargs), ensure_ascii=False), content_type='application/json') | |
return do_the_job |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment