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接口的更多相关文章

  1. 【Python】djangorestframework 基于django框架的接口开发

    官网:http://www.django-rest-framework.org/#installation 下载:https://pypi.python.org/pypi/djangorestfram ...

  2. Python OS模块标准库的系统接口及操作方法

    Python OS模块标准库的系统接口及操作方法 os.name 返回当前操作系统名,定义了'posix','nt','mac','os2','ce','java'(我使用win7/python3.1 ...

  3. Python网络编程——编写一个简单的回显客户端/服务器应用

    今天将python中socket模块的基本API学习完后,照着书上的实例编写一个套接字服务器和客户端.采用python3.5版本,在注释中会标明python2和python3的不同之处. 1.代码 ( ...

  4. 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试

    相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控 用python + hadoop streami ...

  5. 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控

    写在前面 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试 用python + hado ...

  6. 用python + hadoop streaming 编写分布式程序(三) -- 自定义功能

    又是期末又是实训TA的事耽搁了好久……先把写好的放上博客吧 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍 ...

  7. Linux 下Python调用C++编写的动态库

    在工程中用到使用Python调用C++编写的动态库,结果报如下错误: OSError: ./extract_str.so: undefined symbol: _ZNSt8ios_base4InitD ...

  8. vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete)

    vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete) 手把手式笔记 Axios配置 安装 axios ...

  9. python调用腾讯云短信接口

    目录 python调用腾讯云短信接口 账号注册 python中封装腾讯云短信接口 python调用腾讯云短信接口 账号注册 去腾讯云官网注册一个腾讯云账号,通过实名认证 然后开通短信服务,创建短信应用 ...

随机推荐

  1. python测试开发django-rest-framework-64.序列化(serializers.Serializer)

    前言 REST framework中的serializers与Django的Form和ModelForm类非常像.我们提供了一个Serializer类,它为你提供了强大的通用方法来控制响应的输出, 以 ...

  2. 安卓Termux安装ssh及jupyter编程

    软件名称:Termux ssh安装 安装openssh apt update apt install openssh 启动ssh服务 sshd 配置公钥私钥 将电脑的公钥(id_rsa.pub)放入/ ...

  3. nginx安装记录

    1.下载nginx http://nginx.org/en/download.html         下载稳定版本,以nginx/Windows-1.12.2为例,直接下载 nginx-1.12.2 ...

  4. Import declarations are not supported by current JavaScript version

    原因为:不支持当前的js版本,在perference中进行设置javascript的版本即可 注意:在perference中进行更改,而不是defeaut perference,快捷键操作为:comm ...

  5. 开箱一个docker

    1.docker 的出现? 1.1.环境切换配置麻烦 通常我们在开发环境写好代码,打个war/jar包,扔到tomcat下,就算是跑起来了:但是扔到生产环境就挂了,what?各种错误... 1.2.应 ...

  6. 2019.12.07 java计算

    class Demo05{ public static void main(String[] args) { int a=1; a++; int b=1 + a++ + a + a++; System ...

  7. Omnibus 安装

    使用gem gem install omnibus 说明 可能需要配置gem source ,通过 gem source list 可以进行检查 参考如下:   gem source -r https ...

  8. Linux 系统管理——引导过程与服务控制

    一. 系统引导流程 1.开机自检(BIOS)(基本的输入输出系统) 2.MBR引导1.2. MBRIS 当从本机硬盘中启动系统时,首先根据硬盘第一个扇区中MBR (Master Boot Record ...

  9. Zotero使用教程(2)-数据备份

    小书匠 这篇文章的目标是让你无论是 换系统,重新安装zotero等都可以还原回你的文献库,而且整个过程基本是自动完成的. 这部分解决下面的两种情况: 1.zotero有自己既定的一套存储方式,不是一般 ...

  10. 解决IE报错[vue router]Failed to resolve async component default:strict 模式下不允许分配到只读属性

    之前遇到过一个奇怪的问题,在其他浏览器下一切正常,但在万恶的IE下,却一直不行. 具体问题场景就是:比如orderDetail页面出现问题,那么只要是路由跳转的,点第1次无法跳转,必须得点第2次才可以 ...