zoukankan      html  css  js  c++  java
  • day_70练习

    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
    
        def plural_get(self):
            car_query = models.Car.objects.values('title','price','img','info')
            # print(type(car_query))
            car_list = list(car_query)
            return car_list
        
    	def get(self,request,*args,**kwargs):
            pk = kwargs.get("pk")
    
    	if pk:
                car = self.single_get(pk)
                return JsonResponse({
                    'status':0,
                    'msg':'solo get ok',
                    'car':car
                })
            else:
                car_list=self.plural_get()
                return JsonResponse({
                    'status':0,
                    'msg':'mute get ok',
                    'car_list':car_list,
                })
    
    	def post(self,request,*args,**kwargs):
            pk = kwargs.get("pk")
            print(pk)
            if pk:
                return JsonResponse({
                    'status':0,
                    'msg':'solo post ok',
                })
            else:
                return JsonResponse({
                    'status':0,
                    'msg':'mute post ok',
                })
    
    	def put(self,request,*args,**kwargs):
            pk = kwargs.get("pk")
            if pk:
                return JsonResponse({
                    'status':0,
                    'msg':'solo put ok',
                })
            else:
                return JsonResponse({
                    'status':0,
                    'msg':'mute put ok',
                })
    
    	def patch(self, request,*args, **kwargs):
            pk = kwargs.get("pk")
            if pk:
                return JsonResponse({
                    'status': 0,
                    'msg': 'solo patch ok',
                })
            else:
                return JsonResponse({
                    'status': 0,
                    'msg': 'mute patch ok',
                })
    
    	def delete(self,request, *args, **kwargs):
            pk = kwargs.get("pk")
            if pk:
                return JsonResponse({
                    'status': 0,
                    'msg': 'solo delete ok',
                })
            else:
                return JsonResponse({
                    'status': 0,
                    'msg': 'mute delete ok',
                })
    
    
    #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)
    
    
  • 相关阅读:
    (转)Hibernate 注解详解
    PHP - jsPDF+html2canvas实现网页截图导出PDF
    (转)PHP-Sublime Text3插件&快捷键(MAC)
    POI
    maven中使用jstl
    树莓派使用pptpd搭建PPTP服务器
    树莓派网络对时
    Debian 安装 oracle java
    让树莓派(RaspberryPi)显示中文
    树莓派搭建minecraft服务器
  • 原文地址:https://www.cnblogs.com/shaozheng/p/12088710.html
Copyright © 2011-2022 走看看