day_70:

#api / urls:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^cars/$',views.Cars.as_view())
url(r'^cars/(?P<pk>\d+)/$', views.Cars.as_view())
]
#views:
from django.http import JsonResponse
from django.views import View
from . import models class Car(View):

def single_get(self,pk):

car_dic = models.Car.objects.filter(pk=pk).values('title','price','img','info').first()

return car_dic
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">plural_get</span><span class="hljs-params">(self)</span>:</span>
car_query = models.Car.objects.values(<span class="hljs-string">'title'</span>,<span class="hljs-string">'price'</span>,<span class="hljs-string">'img'</span>,<span class="hljs-string">'info'</span>)
<span class="hljs-comment"># print(type(car_query))</span>
car_list = list(car_query)
<span class="hljs-keyword">return</span> car_list <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self,request,*args,**kwargs)</span>:</span>
pk = kwargs.get(<span class="hljs-string">"pk"</span>) <span class="hljs-keyword">if</span> pk:
car = self.single_get(pk)
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>:<span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>:<span class="hljs-string">'solo get ok'</span>,
<span class="hljs-string">'car'</span>:car
})
<span class="hljs-keyword">else</span>:
car_list=self.plural_get()
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>:<span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>:<span class="hljs-string">'mute get ok'</span>,
<span class="hljs-string">'car_list'</span>:car_list,
}) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self,request,*args,**kwargs)</span>:</span>
pk = kwargs.get(<span class="hljs-string">"pk"</span>)
print(pk)
<span class="hljs-keyword">if</span> pk:
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>:<span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>:<span class="hljs-string">'solo post ok'</span>,
})
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>:<span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>:<span class="hljs-string">'mute post ok'</span>,
}) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">put</span><span class="hljs-params">(self,request,*args,**kwargs)</span>:</span>
pk = kwargs.get(<span class="hljs-string">"pk"</span>)
<span class="hljs-keyword">if</span> pk:
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>:<span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>:<span class="hljs-string">'solo put ok'</span>,
})
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>:<span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>:<span class="hljs-string">'mute put ok'</span>,
}) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">patch</span><span class="hljs-params">(self, request,*args, **kwargs)</span>:</span>
pk = kwargs.get(<span class="hljs-string">"pk"</span>)
<span class="hljs-keyword">if</span> pk:
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>: <span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>: <span class="hljs-string">'solo patch ok'</span>,
})
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>: <span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>: <span class="hljs-string">'mute patch ok'</span>,
}) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete</span><span class="hljs-params">(self,request, *args, **kwargs)</span>:</span>
pk = kwargs.get(<span class="hljs-string">"pk"</span>)
<span class="hljs-keyword">if</span> pk:
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>: <span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>: <span class="hljs-string">'solo delete ok'</span>,
})
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">return</span> JsonResponse({
<span class="hljs-string">'status'</span>: <span class="hljs-number">0</span>,
<span class="hljs-string">'msg'</span>: <span class="hljs-string">'mute delete ok'</span>,
})

#models
class Car(models.Model):
title = models.CharField(max_length=64)
price = models.DecimalField(max_digits=5, decimal_places=2)
img = models.FileField(upload_to='avatar/',default='static/img/default.jpg')
info = models.CharField(max_length=255)

day70test的更多相关文章

随机推荐

  1. js倒计时在移动端的应用

    在移动端测试倒计时,将时间转化为毫秒会在苹果手机上出现NaN ``` //在安卓上这样写可以获取到的 var date = '2017-06-12 13:12:13'; var time = new ...

  2. pd.Panel转化成json,然后再还原回来

    在使用tornado的write时候有一个需求,是将panel转化成json;而接收端再将json还原成panel格式. 尝试了很久,终于实现了. panel1 =pd.Panel({"on ...

  3. Let's Encryt免费SSL证书申请[我司方案]

    Let's Encrypt颁发的证书是目前生产的大多数浏览器都信任的,您只需下载并运行Let's Encrypt客户端来生成一个证书即可. 在颁发证书之前,需要验证您的域名的所有权.首先,在您的主机上 ...

  4. php相关操作

    array_unshift : 数组头部追加 用法如下: $arr = ['demo','dmoa']; array_unshift($arr,'demob'); //在$arr的前面追加demob ...

  5. Batch pk Shell - WindowsBatch与LinuxShell比较 [变量符号和关键字]

    原文地址:WindowsBatch与LinuxShell比较[变量符号和关键字] 一 简单实例1)batch file @echo off rem output helloworld ::  outp ...

  6. div+css对网页进行布局

    div+css对网页进行布局 首先在页面整体上进行div标签划分内容区域,然后再用css进行定位,最后再对相应的区域添加内容. 1.用div将页面划分 拿到网站页面图后,首先将分析页面分为哪几块,然后 ...

  7. python相关软件安装流程图解——虚拟机操作——复制虚拟机主机——CentOS-7-x86_64-DVD-1810

    请先确保已经安装了虚拟机 python相关软件安装流程图解——虚拟机安装——CentOS-7-x86_64-DVD-1810——CentOS-01下载 https://www.cnblogs.com/ ...

  8. 尚学linux课程---9、yum相关操作和知识

    尚学linux课程---9.yum相关操作和知识 一.总结 一句话总结: 如何使用比如163,阿里云给yum配置yum源:去官网,不要百度:直接去官网,有帮助文档的(比如centos的就在centos ...

  9. hive 总结四(优化)

    本文参考:黑泽君相关博客 本文是我总结日常工作中遇到的坑,结合黑泽君相关博客,选取.补充了部分内容. 表的优化 小表join大表.大表join小表 将key相对分散,并且数据量小的表放在join的左边 ...

  10. AtCoder ABC 129F Takahashi's Basics in Education and Learning

    题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_f 题目大意 给定一个长度为 L ,首项为 A,公差为 B 的等差数列 S,将这 L 个数拼起 ...