简体   繁体   English

我们如何将文件从 django 发送到 reactjs 我不需要 RESTFRame 工作?

[英]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.

相关问题 如何将 pdf 文件从 Flask 发送到 ReactJS - How to send a pdf file from Flask to ReactJS Django rest 框架:如何发送和下载 xlsx 文件? - Django rest framework: How do I send and download xlsx file? 如何使用 outlook object 从 Bamboo 发送自定义 Outlook email? 我们需要在 Bamboo 服务器上安装 outlook 吗? - How to send customized Outlook email from Bamboo with outlook object? Do we need outlook to be installed in the Bamboo server? 我如何在ReactJs.Net的JSX文件中使用自定义ReactJs组件 - How do I use a custom ReactJs component inside my JSX file from ReactJs.Net 如何从 ReactJS Web 应用程序向 Django API 发送 POST 请求? - How to send POST request to Django API from ReactJS web app? Django 我们如何将 model 发送到 javascript 并将其渲染到模板上 - Django how do we send a model into javascript and render it onto a template Reactjs:如何将获取的 API 数据从组件发送到动态路由组件? - Reactjs: How do i send a fetched API data from a component to a dynamic route component? 如何登录到我们需要抓取数据的网站内部 - how to logged in inside website from where we need to scrape a data 如何将 django 模型发送到 javascipt? - How do I send a django model to javascipt? 如何从ReactJs .html文件访问.js文件中存储的数组? - How do I access an array stored in a .js file from a ReactJs .html file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM