简体   繁体   中英

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. is there a way to send Data from django to reactjs without restframe work?

you can easily convert dic to json using json.dumps() method.

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:

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

Reference: https://docs.djangoproject.com/en/4.1/ref/request-response/#jsonresponse-objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM