(f)
return to_json
Converts any functions which returns a dictionary to a proper HttpResponse with json content.
Converts any functions which returns a dictionary to a proper HttpResponse with json content.
Converts any functions which returns a dictionary to a proper HttpResponse with json content .
def json_response(f):
    """
    Converts any functions which returns a dictionary to a proper HttpResponse with json content.
    """
    def to_json(request, *args, **kwargs):
        """
        Creates the actual HttpResponse with json content.
        """
        try:
            data = f(request, *args, **kwargs)
        except Exception as exc:
            logger.error(exc)
            data = api_error(msg=f"Error: {exc}")

        payload = json.dumps(data, sort_keys=True, indent=4)
        response = HttpResponse(payload, content_type="application/json")
        if not data:
            response.status_code = 404
            response.reason_phrase = 'Not found'
        return response
    return to_json
def json_response ( f ) : def to_json ( request , * args , * * kwargs ) : """
        Creates the actual HttpResponse with json content.
        """ try : data = f ( request , * args , * * kwargs ) except Exception as exc : logger . error ( exc ) data = api_error ( msg = f"Error: {exc}" ) payload = json . dumps ( data , sort_keys = True , indent = 4 ) response = HttpResponse ( payload , content_type = "application/json" ) if not data : response . status_code = 404 response . reason_phrase = 'Not found' return response return to_json