python:使用Djangorestframework编写post和get接口
1、安装django
pip install django
2、新建一个django工程
python manage.py startproject cainiao_monitor_api
3、新建一个app
python manage.py startapp monitor
4、安装DRF
pip install djangorestframework
5、编写视图函数
views.py
from rest_framework.views import APIView
import json
import cx_Oracle
from django.http import HttpResponse
# Create your views here. class MonitorMsg(APIView):
# 此处方法名只能为post或者get等名称,这个名称就是接口类型
def post(self, request):
"""
查询某一天的数据;
此方法请求类型为post,根据urls.py中的配置,该接口地址为:/monitorMsg,
传入参数方式以下三种都可以:json方式或者form-data或者x-www-form-urlencoded,
request.data['datetime']方式都可以解析到入参的值
:param request:
:return: 返回CAINIAO_MONITOR_MSG表中所有的异常信息
"""
# 如果前台通过form-data格式传入参数,则使用request.POST['key']此方法获取参数的值
# datetime = request.POST['datetime']
# 【推荐】如果前台通过json方式或者form-data或者x-www-form-urlencoded传入参数,则使用request.data['key']方法均可获取对应的value
datetime = request.data['datetime']
print(request.data)
sql = "select * from CAINIAO_MONITOR_MSG t WHERE to_char(t.CREATE_TIME,'yyyy-MM-dd') like '%s' " % datetime
# 连接数据库
conn = cx_Oracle.connect('name', 'pwd', '10.*.*.*:1521/sid')
# 创建游标
cursor = conn.cursor()
# 执行sql语句
cursor.execute(sql)
# 提交数据
conn.commit()
# 获取查询数据类型为list
data = cursor.fetchall()
# 关闭游标
cursor.close()
# 断开数据库连接
conn.close()
# 新建空列表用来放数据
msg_list = []
# 遍历查询到的数据
for i in data:
# 创建空的字典,存放对应的字段信息
msg_dict = {}
# 将查询到的数据作为value对应到字典相应key
msg_dict['id'] = i[0]
msg_dict['scenario_code'] = i[1]
msg_dict['msg'] = i[2]
msg_dict['status'] = i[4]
# 将遍历的数据存放到list中
msg_list.append(msg_dict)
# 定义最终的返回数据样式
res_data = {'count': len(msg_list), 'data': msg_list}
# 将res_data序列化为json对象,并返回
return HttpResponse(json.dumps(res_data), content_type="application/json")
6、编写路由
urls.py
from django.contrib import admin
from django.urls import path
from monitor.views import MonitorMsg urlpatterns = [
path('admin/', admin.site.urls),
# 接口的url:http://127.0.0.1:8000/monitorMsg
path('monitorMsg', MonitorMsg.as_view()),
]
7、启动服务
python manage.py runserver
8、访问测试
python:使用Djangorestframework编写post和get接口的更多相关文章
- 【Python】djangorestframework 基于django框架的接口开发
官网:http://www.django-rest-framework.org/#installation 下载:https://pypi.python.org/pypi/djangorestfram ...
- Python OS模块标准库的系统接口及操作方法
Python OS模块标准库的系统接口及操作方法 os.name 返回当前操作系统名,定义了'posix','nt','mac','os2','ce','java'(我使用win7/python3.1 ...
- Python网络编程——编写一个简单的回显客户端/服务器应用
今天将python中socket模块的基本API学习完后,照着书上的实例编写一个套接字服务器和客户端.采用python3.5版本,在注释中会标明python2和python3的不同之处. 1.代码 ( ...
- 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试
相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控 用python + hadoop streami ...
- 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控
写在前面 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试 用python + hado ...
- 用python + hadoop streaming 编写分布式程序(三) -- 自定义功能
又是期末又是实训TA的事耽搁了好久……先把写好的放上博客吧 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍 ...
- Linux 下Python调用C++编写的动态库
在工程中用到使用Python调用C++编写的动态库,结果报如下错误: OSError: ./extract_str.so: undefined symbol: _ZNSt8ios_base4InitD ...
- vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete)
vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete) 手把手式笔记 Axios配置 安装 axios ...
- python调用腾讯云短信接口
目录 python调用腾讯云短信接口 账号注册 python中封装腾讯云短信接口 python调用腾讯云短信接口 账号注册 去腾讯云官网注册一个腾讯云账号,通过实名认证 然后开通短信服务,创建短信应用 ...
随机推荐
- 项目Alpha冲刺 10
作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 介绍第10天冲刺的项目进展.问题困难和心得体会 1.团队信息 队名:火鸡堂 ...
- springboot 整合Swagger2的使用
Swagger2相较于传统Api文档的优点 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工 ...
- 07. vue-router嵌套路由
嵌套路由用法 1.嵌套路由功能分析 点击父级路由链接显示模板内容 模板内容中又有子级路由链接 点击子级路由链接显示子级模板内容 2.父路由组件模板 父级路由链接 父组件路由填充位 <p> ...
- IntelliJ IDEA自身以及maven项目打包方式
1. Idea自身打包方式 1.1 创建Artifacts 快捷键(Ctrl+Alt+Shift+S)打开项目的Project Structure.在Artifacts创建 接着,指定main cla ...
- postgres高可用学习篇二:通过pgbouncer连接池工具来管理postgres连接
安装pgbouncer yum install libevent -y yum install libevent-devel -y wget http://www.pgbouncer.org/down ...
- 【AirTest自学】AirTest工具介绍和入门学习(一)
==================================================================================================== ...
- mybatis框架-查询用户表中的记录数
之前已经搭建过mybatis框架了,现在我们要用mybatis框架真正的干点事情了. 这是这个简单web项目的整体架构. 我们使用mybatis框架查询用户表中的记录数: 这是用户类: package ...
- 06-Flutter移动电商实战-dio基础_Get_Post请求和动态组件协作
上篇文章中,我们只看到了 dio 的使用方式,但并未跟应用关联起来,所以这一篇将 dio 网络请求与应用界面结合起来,当然这也是为以后的实战作基础准备,基础打牢,我们才能飞速前进. 1.案例说明 我们 ...
- [NPM + React] Prepare a Custom React Hook to be Published as an npm Package
Before we publish our package, we want to make sure everything is set up correctly. We’ll cover vers ...
- LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters ...