在 Class 中转换函数视图基于视图 Django + chartit
Transform function view in Class Based View Django + chartit
我有这个功能视图。
如何在 Class 基于视图中转换此功能?
在这种情况下我使用 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错误
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)
我继续并尽我所能尝试在 Whosebug 中查看它。为什么不使用像 pycharm 这样的 IDE 让生活变得轻松(至少在格式化方面)?
我有这个功能视图。
如何在 Class 基于视图中转换此功能?
在这种情况下我使用 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错误
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)
我继续并尽我所能尝试在 Whosebug 中查看它。为什么不使用像 pycharm 这样的 IDE 让生活变得轻松(至少在格式化方面)?