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']方法均可获取对应的valuedatetime = 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()# 获取查询数据类型为listdata = cursor.fetchall()# 关闭游标
        cursor.close()# 断开数据库连接
        conn.close()# 新建空列表用来放数据msg_list = []# 遍历查询到的数据for i in data:# 创建空的字典,存放对应的字段信息msg_dict = {}# 将查询到的数据作为value对应到字典相应keymsg_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 MonitorMsgurlpatterns = [path('admin/', admin.site.urls),# 接口的url:http://127.0.0.1:8000/monitorMsgpath('monitorMsg', MonitorMsg.as_view()),
]

 

7、启动服务

python manage.py runserver

 

8、访问测试

 

 

 

 

转载于:https://www.cnblogs.com/gcgc/p/11541628.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391224.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Kubernetes 入门(3)集群安装

1. kubeadm简介 kubeadm 是 Kubernetes 官方提供的一个 CLI 工具,可以很方便的搭建一套符合官方最佳实践的最小化可用集群。当我们使用 kubeadm 搭建集群时,集群可以通过 K8S 的一致性测试,并且 kubeadm 还支持其他的集群生命周期功能&#…

Angular Material 攻略 04 Icon

Icon 网页系统中的Icon虽然说很简单,但是其中的学问还是有很多的,我们常用的Icon库有FontAwesome、Iconfont等,我们选择了Angular Material这个组件库,就介绍Material Icons吧。 对Icon感兴趣的同学可以看一下这里 Material Desig…

【9303】平面分割

Time Limit: 10 second Memory Limit: 2 MB 问题描述 同一平面内有n(n≤500)条直线,已知其中p(p≥2)条直线相交与同一点,则这n条直线最多能将平面分割成多少个不同的区域? Input 两个整数n&am…

简述yolo1-yolo3_使用YOLO框架进行对象检测的综合指南-第一部分

简述yolo1-yolo3重点 (Top highlight)目录: (Table Of Contents:) Introduction 介绍 Why YOLO? 为什么选择YOLO? How does it work? 它是如何工作的? Intersection over Union (IoU) 联合路口(IoU) Non-max suppression 非最大抑制 Networ…

django:资源网站汇总

Django REST framework官网 http://www.sinodocs.cn/ django中文网 https://www.django.cn/ 转载于:https://www.cnblogs.com/gcgc/p/11542068.html

Kubernetes 入门(4)集群配置

1. 集群配置 报错: message: ‘runtime network not ready: NetworkReadyfalse reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized’ 原因:cni未被初始化(CNI 是 Container Network In…

【例9.8】合唱队形

【例9.8】合唱队形 链接:http://ybt.ssoier.cn:8088/problem_show.php?pid1264 时间限制: 1000 ms 内存限制: 65536 KB【题目描述】 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形。 合唱队形是…

scrum流程 规划 冲刺_Scrum –困难的部分2:更快地冲刺

scrum流程 规划 冲刺In the first part, I presented my favorite list of Scrums hard parts and how to work around them. In the second part, I offer you a colorful bouquet of workarounds as well. Have fun!在第一部分中 ,我介绍了我最喜欢的Scrum困难部分…

JAVA基础知识|lambda与stream

lambda与stream是java8中比较重要两个新特性,lambda表达式采用一种简洁的语法定义代码块,允许我们将行为传递到函数中。之前我们想将行为传递到函数中,仅有的选择是使用匿名内部类,现在我们可以使用lambda表达式替代匿名内部类。在…

数据库:存储过程_数据科学过程:摘要

数据库:存储过程Once you begin studying data science, you will hear something called ‘data science process’. This expression refers to a five stage process that usually data scientists perform when working on a project. In this post I will walk through ea…

901

901 转载于:https://www.cnblogs.com/Forever77/p/11542129.html

leetcode 137. 只出现一次的数字 II(位运算)

给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。 示例 1: 输入:nums [2,2,3,2] 输出:3 示例 2: 输入:nums [0,1,0,…

【p081】ISBN号码

Time Limit: 1 second Memory Limit: 50 MB 【问题描述】 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号&#xff…

gitlab bash_如何编写Bash一线式以克隆和管理GitHub和GitLab存储库

gitlab bashFew things are more satisfying to me than one elegant line of Bash that automates hours of tedious work. 没有什么比让Bash自动完成数小时繁琐工作的Bash优雅系列令我满意的了。 As part of some recent explorations into automatically re-creating my la…

寒假学习笔记(4)

2018.2.11 类中的常成员 关键字const,在类定义中声明数据成员使用关键字限定,声明时不能初始化。初始化列表,类中的任何函数都不能对常数据成员赋值,包括构造函数。为构造函数添加初始化列表是对常数据成员进行初始化的唯一途径。…

svm和k-最近邻_使用K最近邻的电影推荐和评级预测

svm和k-最近邻Recommendation systems are becoming increasingly important in today’s hectic world. People are always in the lookout for products/services that are best suited for them. Therefore, the recommendation systems are important as they help them ma…

Oracle:时间字段模糊查询

需要查询某一天的数据,但是库里面存的是下图date类型 将Oracle中时间字段转化成字符串,然后进行字符串模糊查询 select * from CAINIAO_MONITOR_MSG t WHERE to_char(t.CREATE_TIME,yyyy-MM-dd) like 2019-09-12 转载于:https://www.cnblogs.com/gcgc/p/…

cogs2109 [NOIP2015] 运输计划

cogs2109 [NOIP2015] 运输计划 二分答案树上差分。 STO链剖巨佬们我不会(太虚伪了吧 首先二分一个答案,下界为0,上界为max{路径长度}。 然后判断一个答案是否可行,这里用到树上差分。 (阔以理解为前缀和??&…

leetcode 690. 员工的重要性(dfs)

给定一个保存员工信息的数据结构,它包含了员工 唯一的 id ,重要度 和 直系下属的 id 。 比如,员工 1 是员工 2 的领导,员工 2 是员工 3 的领导。他们相应的重要度为 15 , 10 , 5 。那么员工 1 的数据结构是 [1, 15, [2]] &#x…

组件分页_如何创建分页组件

组件分页The theme for week #17 of the Weekly Coding Challenge is:每周编码挑战第17周的主题是: 分页 (Pagination) A Pagination Component is used on websites where you have more content available than you want to display at one time to the user so …