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)