基于Django分类的视图__init __()缺少1个必需的位置参数

问题描述 投票:0回答:1

我正在尝试在Django中使用基于类的视图!

我的urls.py:

path('pie-chart/', views.pie_chart, name='pie_chart.html'),

我的view.py

class pie_chart(View):
    def __init__(self, labels, data):
        self.labels = labels
        self.data = data


    def active_sessions(self, request):
        self.labels = []
        self.data = []
        queryset = Employees.objects.values('department__name').annotate(total_session=Count('employeeconnection__employeesession'))
        for item in queryset: 
            self.labels.append(item['department__name'])
            self.data.append(item['total_session'])

        return render(request, 'pie_chart.html', {
            'labels': self.labels,
            'data': self.data,
        })

我收到此错误:

__init__() missing 1 required positional argument: 'data'
django django-views django-class-based-views
1个回答
0
投票
path('pie-chart/', views.pie_chart.as_view(), name='pie_chart.html'),

更正网址

© www.soinside.com 2019 - 2024. All rights reserved.