广告小程序后端开发(7.获取价格表数据)
Posted xuepangzi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了广告小程序后端开发(7.获取价格表数据)相关的知识,希望对你有一定的参考价值。
1.在xadmin后台,手动添加数据:
http://127.0.0.1:8000/xadmin/trade/price/
2.在apps/trade/目录下新建序列化文件serializers.py:
from rest_framework import serializers from .models import Price,Order,ShoppingCar class 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 render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer from .serializers import PriceModelSerializer,OrderModelSerializer,ShoppingCarModelSerializer from .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 path from .views import GetPrice urlpatterns = [ path(‘getprice/‘,GetPrice.as_view()) ]
5.gg/urls.py中:
from django.urls import path,include from django.views.static import serve from gg.settings import MEDIA_ROOT import xadmin urlpatterns = [ #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‘)) ]
以上是关于广告小程序后端开发(7.获取价格表数据)的主要内容,如果未能解决你的问题,请参考以下文章
广告小程序后端开发(16.优惠券系统:原理流程图,奖品实验数据,生成二维码)