I have tried two different style of views: Function-based and class-based. I have two functions in my views.py and i don’t know how to call both of them under single same url. I have seen suggestions to combine both functions into one but it still doesn’t work.
Tried get() from class-based view and called the same url with different views
JavaScript
x
146
146
1
path('home/dashboard/', views.get_profile, name='dashboard'),
2
path('home/dashboard/', views.get_dept, name='dashboard'),
3
4
def get_dept(request, *args, **kwargs):
5
dataset = Department.objects.all()
6
.values('department')
7
.annotate(IT_count=Count('department',
8
filter=Q(department="IT")),
9
Sales_count=Count('department',
10
filter=Q(department="Sales")),
11
Admin_count=Count('department',
12
filter=Q(department="Admin")),
13
HR_count=Count('department',
14
filter=Q(department="HR")))
15
.order_by('department')
16
17
categories = list()
18
IT_series_data = list()
19
Sales_series_data = list()
20
Admin_series_data = list()
21
HR_series_data = list()
22
23
for entry in dataset:
24
categories.append('%s Department' % entry['department'])
25
IT_series_data.append(entry['IT_count'])
26
Sales_series_data.append(entry['Sales_count'])
27
Admin_series_data.append(entry['Admin_count'])
28
HR_series_data.append(entry['HR_count'])
29
30
IT_series = {
31
'name': 'IT',
32
'data': IT_series_data,
33
'color': 'green'
34
}
35
36
Sales_series = {
37
'name': 'Sales',
38
'data': Sales_series_data,
39
'color': 'yellow'
40
}
41
42
Admin_series = {
43
'name': 'Admin',
44
'data': Admin_series_data,
45
'color': 'red'
46
}
47
48
HR_series = {
49
'name': 'HR',
50
'data': HR_series_data,
51
'color': 'blue'
52
}
53
54
chart2 = {
55
'chart': {
56
'type': 'column',
57
'backgroundColor': '#E3F0E6',
58
'option3d': {
59
'enabled': "true",
60
'alpha': 10,
61
'beta': 15,
62
'depth': 50,
63
}
64
},
65
'title': {'text': 'Containers per department'},
66
'xAxis': {'categories': categories},
67
'yAxis': {
68
'title': {
69
'text': 'No.of containers'},
70
'tickInterval': 1
71
},
72
'plotOptions': {
73
'column': {
74
'pointPadding': 0.2,
75
'borderWidth': 0,
76
'depth': 60,
77
}
78
},
79
'series': [IT_series, Sales_series, Admin_series, HR_series],
80
'colorByPoint': "true",
81
}
82
83
dump2 = json.dumps(chart2)
84
85
return render(request, 'accounts/dashboard.html', {'chart2': dump2})
86
87
88
def get_profile(request, *args, **kwargs):
89
dataset = Profile.objects
90
.values('is_active')
91
.annotate(is_active_count=Count('is_active', filter=Q(is_active=True)),
92
not_is_active_count=Count('is_active', filter=Q(is_active=False)))
93
94
# categories = list('User')
95
is_active_series_data = list()
96
not_is_active_series_data = list()
97
98
for entry in dataset:
99
# categories.append('User')
100
is_active_series_data.append(entry['is_active_count'])
101
not_is_active_series_data.append(entry['not_is_active_count'])
102
103
is_active_series = {
104
'name': 'Active user',
105
'data': is_active_series_data,
106
'color': '#23CE3F'
107
}
108
109
not_is_active_series = {
110
'name': 'Inactive user',
111
'data': not_is_active_series_data,
112
'color': '#FB3A3A'
113
}
114
115
chart = {
116
'chart': {
117
'type': 'column',
118
'backgroundColor': '#E3F0E6',
119
'options3d': {
120
'enabled': "true",
121
'alpha': 10,
122
'beta': 15,
123
'depth': 50,
124
}
125
},
126
'title': {'text': 'Active user on Current Platform'},
127
'xAxis': {'categories': ['Active', 'Inactive']},
128
'yAxis': {
129
'title': {
130
'text': 'No.of users'},
131
'tickInterval': 1
132
},
133
'plotOptions': {
134
'column': {
135
'pointPadding': 0.2,
136
'borderWidth': 0,
137
'depth': 60,
138
}
139
},
140
'series': [is_active_series, not_is_active_series]
141
}
142
143
dump = json.dumps(chart)
144
145
return render(request, 'accounts/dashboard.html', {'chart': dump})
146
Advertisement
Answer
There is no way in django to put two views against one url. Maybe you can combine those functions in one Class based view and its pretty simple actually. For example:
JavaScript
1
26
26
1
# view
2
from django.views.generic import TemplateView
3
4
5
class DashboardView(TemplateView):
6
template_name = 'accounts/dashboard.html'
7
8
def get_dept(self):
9
# rest of the code....
10
dump2 = json.dumps(chart2)
11
return dump2
12
13
def get_profile(self):
14
# rest of the code....
15
dump = json.dumps(chart)
16
return dump
17
18
def get_context_data(self, *args, **kwargs):
19
context = super(ChartView, self).get_context_data(*args, **kwargs)
20
context['chart'] = self.get_profile()
21
context['chart2'] = self.get_dept()
22
return context
23
24
# url
25
path('home/dashboard/', views.DashboardView.as_view(), name='dashboard'),
26
And in template, you should get the values in {{ chart }}
and {{ chart2 }}
variables.