use JsonResponse instead of HttpResponse to send JSON data

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: python-django/http-response-with-json-dumps

Language: Python

Severity: Notice

Category: Best Practices

Description

Use JsonResponse instead of HttpResponse when attempting to send JSON data.

Non-Compliant Code Examples

import json

from django.http import HttpResponse

response_data = {}
response_data['result'] = 'error'
response_data['message'] = 'Some error message'
return HttpResponse(json.dumps(response_data))  # use a JsonResponse to send JSON data

Compliant Code Examples

from django.http import JsonResponse

response_data = {}
response_data['result'] = 'error'
response_data['message'] = 'Some error message'
return JsonResponse(response_data)
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: safchain/fix-custom-agent