2015-05-22 78 views
1

我有这个函数视图。在基于类的视图中转换函数视图Django + chartit

如何在基于类的视图中转换此功能?

在这种情况下,我使用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}) 

返回错误

enter image description here

+1

1天有人会杀了你这个代码格式化 madzohan

回答

2
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 

在URL中应该是这样的

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

看到我的github https://github.com/rg3915/chartit/commit/9dd195db3888e20b3fb06b7efd912f2ba2f1aaed –

+0

您get_water_chart不返回任何东西......添加“回归”之前'图表('https://github.com/rg3915/chartit/blob/9dd195db3888e20b3fb06b7efd912f2ba2f1aaed/myproject/core/views.py#L38 – madzohan

+0

谢谢,完美的解决方案。但可惜的是,在Python 3中运行的chartit –

0

我想你最好的选择是使用一个通用视图代替一个模板,因为它很容易做出开关。就像:

from django.shortcuts import get_object_or_404 
from django.shortcuts import render 
from django.views.generic import View 

class LinechartView(View): 
    def get(self, request, *args, **kwargs): 
    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(request, {'weatherchart': cht}) 
    # Doing it like this also allows flexibility to add things like a post easily as well 
    # Here's an example of how'd you go about that 

    def post(self, request, *args, **kwargs): 
    # Handles updates of your model object 
    other_weather = get_object_or_404(YourModel, slug=kwargs['slug']) 
    form = YourForm(request.POST) 
    if form.is_valid(): 
     form.save() 
    return redirect("some_template:detail", other_weather.slug)  

我继续前进,并格式化到我的能力,尽力查看它在stackoverflow中。为什么不使用像pycharm这样的IDE来让生活变得轻松(至少对于格式化)?