简体   繁体   English

如何使用Web API将数据插入Django应用数据库

[英]How to insert data into django app database using a web API

On a server, I have a Django app with a database. 在服务器上,我有一个带数据库的Django应用。 At the client, I am writing a Python script to send data through https to the server and have the server insert that data into the database. 在客户端,我正在编写一个Python脚本,以通过https将数据发送到服务器,并让服务器将这些数据插入数据库中。

How should I approach this problem? 我应该如何解决这个问题? I am in control of both the server and the client. 我可以控制服务器和客户端。 The reason for wanting to do this is to have an API to insert data into the database. 想要这样做的原因是拥有一个将数据插入数据库的API。

You can send data from the Python Script to the backend. 您可以将数据从Python脚本发送到后端。 You will need to have in place an API defined in Django, of course. 当然,您将需要在Django中定义一个API。 But assuming you already have it you can simply use the awesome requests library to send the data from the Script: 但是,假设您已经拥有了它,则可以简单地使用很棒的请求库来从脚本发送数据:

>>> r = requests.get('http://localhost:8000/v1/api/resource?param1=12&param2=blabla')

Or using POST: 或使用POST:

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.post('http://localhost:8000/v1/api/resource', data=payload)

And the Backend will receive it (If you are using Django you will need to process it in the View, and most probably use the ORM to store the value). 后端将接收它(如果您使用的是Django,则需要在View中对其进行处理,并且很可能使用ORM来存储值)。 If by any chance you don't receive the value in the View, it means that the APIs defined in the urls.py file are probably not configured correctly. 如果您没有在View中收到该值,则意味着urls.py文件中定义的API可能未正确配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Django将龙卷风Web套接字中的数据插入到MySQL数据库中 - How to insert data from a tornado web-socket to a mySQL database using Django 如何在Django和Python中使用准备好的语句查询将数据插入数据库 - How to insert data into database using prepared statement query in Django and Python 无法使用 Django 将数据插入数据库 - Not able to insert data into database using Django 如何在 Django 中将 JSON 数据插入数据库? - How to insert JSON data to database in Django? 如何在没有表单的情况下将数据插入到 django 数据库中 - How to insert data to django database without forms Django/React 应用程序:如何在我的 web 应用程序中兼容地同时拥有 Django URLS(用于 api 发送数据)和 React URLS(用于呈现我的组件) - Django/React App: How to have both Django URLS (for the api to send data) and React URLS (to render my components) in my web app, compatibly 如何使用 python django 将 html 表单中的数据插入 MySQL 数据库 - How to insert data from html form into MySQL database using python django 如何使用 Python Django 将动态生成的行中的多行数据插入数据库 - How to insert multiple rows of data from dynamically generated rows into a database using Python Django 在 Django Web 框架中使用 TMDb API 来检索数据库中电影的海报 - Using TMDb API in Django Web Framework to retrieve posters for movies in a database 如何使用 Pandas 将数据插入 Oracle 数据库? - How to insert data into Oracle Database using Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM