[英]how do we send file from django to reactjs where i dont need RESTFRame work?
I am new to ReactJS,I am using a django as backend, React as frontend i need to send a dict/json to reactjs which is locally created by choices not from data base.我是 ReactJS 的新手,我使用 django 作为后端,React 作为前端,我需要将 dict/json 发送到 reactjs,而不是本地创建的数据。 is there a way to send Data from django to reactjs without restframe work?有没有办法在没有休息框架的情况下将数据从 django 发送到 reactjs?
you can easily convert dic to json using json.dumps()
method.您可以使用json.dumps()
方法轻松地将 dic 转换为 json。
import json
# Data to be written
dictionary ={
"id": "04",
"name": "sunil",
"department": "HR"
}
# Serializing json
json_object = json.dumps(dictionary, indent = 4)
You can use JsonResponse instead of ordinary HttpResponse:您可以使用 JsonResponse 代替普通的 HttpResponse:
from django.http import JsonResponse
def hello_dictionary(request):
data = {'name': 'Alison', 'grade': 9}
return JsonResponse(data)
def hello_list(request):
data = [{'name': 'Alison', 'grade':9}, {'name': 'Alice', 'grade': 8}, {'name': 'Tom', 'grade': 10}]
return JsonResponse(data, safe=False)
If you pass something that is not a dictionary remember to add safe=false如果您传递的不是字典,请记住添加safe=false
Reference: https://docs.djangoproject.com/en/4.1/ref/request-response/#jsonresponse-objects参考: https://docs.djangoproject.com/en/4.1/ref/request-response/#jsonresponse-objects
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.