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‘)) ]
原文地址:https://www.cnblogs.com/xuepangzi/p/10888019.html
时间: 2024-10-12 06:23:27