【问题标题】:Transform function view in Class Based View Django + chartit在基于类的视图 Django + chartit 中转换函数视图
【发布时间】:2015-05-22 20:17:47
【问题描述】:

我有这个函数视图。

如何在基于类的视图中转换这个函数?

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

返回错误

【问题讨论】:

  • 有一天有人会因为这种代码格式化而杀了你

标签: django function charts django-generic-views


【解决方案1】:
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应该是这样的

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

【讨论】:

  • 你的 get_water_chart 没有返回任何东西...在 'Chart(' github.com/rg3915/chartit/blob/… 之前添加 'return'
  • 谢谢,完美的解决方案。但遗憾的是 chartit 没有在 Python 3 中运行
  • 将 repo 保存到您的项目并为 python 3 进行更改(在大多数情况下,对于小包,它是 python3porting.com/differences.html
  • 我更喜欢使用 hicharts.js 并手动序列化数据...没有 django-chartit :D ...但您可以尝试将其移植到 python 3
【解决方案2】:

我认为您最好的选择是使用通用视图而不是模板,因为它很容易进行切换。比如:

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 来简化生活(至少在格式化方面)?

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 2023-03-06
    • 2014-02-16
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    相关资源
    最近更新 更多