Search code examples
djangofunctionchartsdjango-generic-views

Transform function view in Class Based View Django + chartit


I have this function view.

How to transform this function in Class Based View?

In this case i use TemplateView?

def linechart(request):
    ds = DataPool(
        series=[{'options': {
            'source': MonthlyWeatherByCity.objects.all()},
            'terms': [
            'month',
            'houston_temp',
            'boston_temp']}
        ])

    cht = Chart(
        datasource=ds,
        series_options=[{'options': {
            'type': 'bar',
            'stacking': False},
            'terms': {
            'month': [
                'boston_temp',
                'houston_temp']
        }}],
        chart_options={'title': {
            'text': 'Weather Data of Boston and Houston'},
            'xAxis': {
            'title': {
                'text': 'Month number'}}})

    return render_to_response('core/linechart.html', {'weatherchart': cht})

Return error

enter image description here


Solution

  • class MyTemplateView(TemplateView):
        template_name = 'core/linechart.html'
    
        def get_ds(self):
            return DataPool(...)
    
        def get_water_chart(self):
            return Chart(datasource=self.get_ds() ...)
    
        def get_context_data(self, **kwargs):
            context = super(MyTemplateView, self).get_context_data(**kwargs)
            context['weatherchart'] = self.get_water_chart()
    
            return context
    

    in urls should be something like this

    url(r'^$', MyTemplateView.as_view(), name='index'),