Skip to content
Advertisement

Is there a way to call two function-based views from views.py from one url? – Django or even by using Class-based view

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

    path('home/dashboard/', views.get_profile, name='dashboard'),
    path('home/dashboard/', views.get_dept, name='dashboard'),

         def get_dept(request, *args, **kwargs):
            dataset = Department.objects.all() 
                   .values('department') 
                   .annotate(IT_count=Count('department', 
                           filter=Q(department="IT")),
                           Sales_count=Count('department', 
                           filter=Q(department="Sales")),
                           Admin_count=Count('department', 
                           filter=Q(department="Admin")),
                           HR_count=Count('department', 
                           filter=Q(department="HR"))) 
                   .order_by('department')

        categories = list()
        IT_series_data = list()
        Sales_series_data = list()
        Admin_series_data = list()
        HR_series_data = list()

        for entry in dataset:
            categories.append('%s Department' % entry['department'])
            IT_series_data.append(entry['IT_count'])
            Sales_series_data.append(entry['Sales_count'])
            Admin_series_data.append(entry['Admin_count'])
            HR_series_data.append(entry['HR_count'])

        IT_series = {
            'name': 'IT',
            'data': IT_series_data,
            'color': 'green'
        }

        Sales_series = {
            'name': 'Sales',
            'data': Sales_series_data,
            'color': 'yellow'
        }

        Admin_series = {
            'name': 'Admin',
            'data': Admin_series_data,
            'color': 'red'
        }

        HR_series = {
            'name': 'HR',
            'data': HR_series_data,
            'color': 'blue'
        }

        chart2 = {
            'chart': {
                'type': 'column',
                'backgroundColor': '#E3F0E6',
                'option3d': {
                    'enabled': "true",
                    'alpha': 10,
                    'beta': 15,
                    'depth': 50,
                }
            },
            'title': {'text': 'Containers per department'},
            'xAxis': {'categories': categories},
            'yAxis': {
                'title': {
                    'text': 'No.of containers'},
                'tickInterval': 1
                    },
            'plotOptions': {
                'column': {
                    'pointPadding': 0.2,
                    'borderWidth': 0,
                    'depth': 60,
                }
            },
            'series': [IT_series, Sales_series, Admin_series, HR_series],
            'colorByPoint': "true",
        }

        dump2 = json.dumps(chart2)

        return render(request, 'accounts/dashboard.html', {'chart2': dump2})


def get_profile(request, *args, **kwargs):
        dataset = Profile.objects 
            .values('is_active') 
            .annotate(is_active_count=Count('is_active', filter=Q(is_active=True)),
                      not_is_active_count=Count('is_active', filter=Q(is_active=False))) 

        # categories = list('User')
        is_active_series_data = list()
        not_is_active_series_data = list()

        for entry in dataset:
            # categories.append('User')
            is_active_series_data.append(entry['is_active_count'])
            not_is_active_series_data.append(entry['not_is_active_count'])

        is_active_series = {
            'name': 'Active user',
            'data': is_active_series_data,
            'color': '#23CE3F'
        }

        not_is_active_series = {
            'name': 'Inactive user',
            'data': not_is_active_series_data,
            'color': '#FB3A3A'
        }

        chart = {
            'chart': {
                'type': 'column',
                'backgroundColor': '#E3F0E6',
                'options3d': {
                    'enabled': "true",
                    'alpha': 10,
                    'beta': 15,
                    'depth': 50,
                }
            },
            'title': {'text': 'Active user on Current Platform'},
            'xAxis': {'categories': ['Active', 'Inactive']},
            'yAxis': {
                'title': {
                    'text': 'No.of users'},
                'tickInterval': 1
                    },
            'plotOptions': {
                'column': {
                    'pointPadding': 0.2,
                    'borderWidth': 0,
                    'depth': 60,
                }
            },
            'series': [is_active_series, not_is_active_series]
        }

        dump = json.dumps(chart)

        return render(request, 'accounts/dashboard.html', {'chart': dump})

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:

# view
from django.views.generic import TemplateView


class DashboardView(TemplateView):
     template_name = 'accounts/dashboard.html'

     def get_dept(self):
        # rest of the code....
        dump2 = json.dumps(chart2)
        return dump2

    def get_profile(self):
        # rest of the code....
        dump = json.dumps(chart)
        return dump

    def get_context_data(self, *args, **kwargs):
        context = super(ChartView, self).get_context_data(*args, **kwargs)
        context['chart'] = self.get_profile()
        context['chart2'] = self.get_dept()
        return context

# url
path('home/dashboard/', views.DashboardView.as_view(), name='dashboard'),

And in template, you should get the values in {{ chart }} and {{ chart2 }} variables.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement