先说一下传统的前后端分离,,
- class GoodsListView(View):
- def get(self, request):
- """
- 通过 django 的 view 实现商品列表页
- :param request:
- :return:
- """
- json_list = []
- goods = Goods.objects.all()[:10]
- for good in goods:
- json_dict = {}
- json_dict["name"] = good.name
- json_dict["category"] = good.category.name
- json_dict["market_price"] = good.market_price
- json_dict["add_time"] = good.add_time
- json_list.append(json_dict)
- # 导入 HttpResponse, JsonResponse 模块
- # 导入 JSON
- import JSON
- from django.http import HttpResponse, JsonResponse
- return HttpResponse(JSON.dumps(json_list),content_type="application")
- View Code
配置一下访问路由, 先导入相应视图
浏览器安装 JSON,vive
痛点:
不能对 tim 序例化, 填充对象, 类似于下面的步聚太多, 容易出错
- json_dict["category"] = good.category.name
- json_dict["market_price"] = good.market_price
- json_dict["add_time"] = good.add_time
所以下面我们使用
来源: http://www.bubuko.com/infodetail-2846187.html