广告小程序后端开发(7.获取价格表数据)

1.在xadmin后台,手动添加数据:

http://127.0.0.1:8000/xadmin/trade/price/

技术图片

2.在apps/trade/目录下新建序列化文件serializers.py:

from rest_framework import serializersfrom .models import Price,Order,ShoppingCarclass PriceModelSerializer(serializers.ModelSerializer): class Meta: model = Price fields="__all__"class OrderModelSerializer(serializers.ModelSerializer): class Meta: model = Order fields="__all__" class ShoppingCarModelSerializer(serializers.ModelSerializer): class Meta: model = ShoppingCar fields="__all__"

3.在apps/trade/views.py中编写获取价格表数据的视图类:

from django.shortcuts import renderfrom rest_framework.views import APIViewfrom rest_framework.response import Responsefrom rest_framework.renderers import JSONRenderer,BrowsableAPIRendererfrom .serializers import PriceModelSerializer,OrderModelSerializer,ShoppingCarModelSerializerfrom .models import Price,Order,ShoppingCar# Create your views here.class GetPrice(APIView): """ 获取价格表数据 """ renderer_classes = [JSONRenderer] # 渲染器 def get(self,request): price_list = Price.objects.all() re = PriceModelSerializer(price_list, many=True) return Response(re.data)

4.在apps/trade/目录下新建urls.py:

from django.urls import pathfrom .views import GetPriceurlpatterns = [ path(getprice/,GetPrice.as_view())]

5.gg/urls.py中:

from django.urls import path,includefrom django.views.static import servefrom gg.settings import MEDIA_ROOTimport xadminurlpatterns = [ #path(‘admin/‘, admin.site.urls), path(media/<path:path>,serve,{document_root:MEDIA_ROOT}), path(xadmin/, xadmin.site.urls), path(users/,include(users.urls)), path(ad/,include(ad.urls)), path(trade/,include(trade.urls))]

 

相关文章