在基于类的视图Django+;查特
我有这个函数视图 如何在基于类的视图中转换此函数 在这种情况下,我使用TemplateView在基于类的视图Django+;查特,django,function,charts,django-generic-views,Django,Function,Charts,Django Generic Views,我有这个函数视图 如何在基于类的视图中转换此函数 在这种情况下,我使用TemplateView def linechart(request): ds = DataPool( series=[{'options': { 'source': MonthlyWeatherByCity.objects.all()}, 'terms': [ 'month', 'houston_temp'
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})
返回错误
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'),
我认为最好的选择是使用通用视图而不是模板,因为切换非常容易。比如:
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让生活变得简单(至少在格式化方面)?有一天,有人会因为这种代码格式化而杀了你看我的github你的get\u water\u图表不会返回任何东西。。。在“Chart”之前添加“return”(“谢谢,完美的解决方案。但遗憾的是,chartit没有在Python 3save repo中运行到您的项目中,并对Python 3进行更改(在大多数情况下,对于小型软件包,我更喜欢使用hicharts.js并手动序列化数据…而不使用django chartit:D…但您可以尝试将其移植到Python 3中