zoukankan      html  css  js  c++  java
  • drf作业01

    apiurls

    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()),
        url(r'^cars/(?P<pk>d+)/$', views.Cars.as_view())
    ]
    

    views

    from django.http import JsonResponse
    from django.views import  View
    from . import  models
    # Create your views here.
    class Cars(View):
    
        def _single_get(self,pk):
            car_dic = models.Car.objects.filter(pk=pk).values('title','price','img','info').first()
            return car_dic
    
        def _mult_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")
            # print(pk)
    
            if pk:
                car = self._single_get(pk)
                return JsonResponse({
                    'status':0,
                    'msg':'solo get ok',
                    'car':car
                })
            else:
                car_list=self._mult_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',
                })
    
    # post(title,,,)
    # car_obj = models.Car(title,,,)
    # car_obj.save()
    #patch(pk,price)
    #car_obj= get_car(pk=pk).update(price=price)
    #delete(pk)
    # models.Car.objects.filter(pk=pk).delete()
    
    
  • 相关阅读:
    [BZOJ2839:]集合计数
    [BZOJ2863:]愤怒的元首
    [BZOJ:3162]:独钓寒江雪
    PHP数据库基础(简单的)
    PHP数组创建和遍历(基础)
    中缀表达式转换为前、后缀表达式转化简单的技巧[转]
    PHP网页简单的计算机源代码
    JS确认取消按钮使用
    js(JavaScript)使用${pageContext.request.contextPath}报错
    易游验证怎么配置?易游验证怎么使用!!
  • 原文地址:https://www.cnblogs.com/agsol/p/12088257.html
Copyright © 2011-2022 走看看