老师,我这里view中使用函数的话一切正常,使用类就报“TypeError: init() takes 1 positional argument but 2 were given”这个错误
import json
from django.http import HttpResponse
from goods.models import Goods
def GoodsListView(request):
json_list = []
goods = Goods.objects.all()[:10]
for good in goods:
json_dict = {}
json_dict["name"] = good.name
json_dict["market_price"] = good.shop_price
json_list.append(json_dict)
return HttpResponse(json.dumps(json_list),content_type="application/json")
import json
from django.http import HttpResponse
from goods.models import Goods
from django.views import View
class GoodsListView(View):
def get(self, request):
json_list = []
goods = Goods.objects.all()[:10]
for good in goods:
json_dict = {}
json_dict["name"] = good.name
json_dict["market_price"] = good.shop_price
# json_dict["add_time"] = good.add_time
json_list.append(json_dict)
return HttpResponse(json.dumps(json_list),content_type="application/json")
from django.views.generic.base import View
?
不知道是什么问题?
Django REST framework课程视频,RESTFul API前后端分离开发
了解课程