我正在学习django,我正在尝试在views.py文件中创建自己的自定义类
这个类我将使用方法,一个用于经典HTML呈现,另一个用于json响应
我在views.py中的课程
class myListView():
context = {}
def __init__(self, request):
request = request
context['PageTitle'] = 'Contacts'
context['people'] = People.objects.all()
def htmlRender(self, *args, **kwargs):
context['children_template'] = 'people/list.html'
return render(request,'base.html',context)
def jsonRender(self, *args, **kwargs):
return HttpResponse(json.dumps(self.context['people']), content_type="application/json")
我的urls.py
path('list', login_required(myListView.htmlRender()), name='list'),
path('list/json', login_required(myListView.jsonRender()), name='list'),
这是调试器发出的错误:
TypeError: htmlRender() missing 1 required positional argument: 'self'
我不知道如何解决这个问题,也许我梦想在视图中使用自定义类?
谢谢
from django.views.generic import ListView
class myListView(ListView):
也许你没有扩展ListView类,试试这个。
您应该首先从“myListView”类创建一个实例,然后使用它:
myListViewInstance = myListView(arguments)
myListViewInstance.htmlRender()