1

我的 django views.py 文件中有一个 dayarchiveview

class day_archive(DayArchiveView):
    model=Timer
    paginate_by=12
    allow_future =True
    allow_empty=True
    month_format = '%m'
    day_format='%d'
    date_field='start_hour'
    template_name='timer/timer_archive_date'

    def get_queryset(self):
        return Timer.objects.filter(author=self.request.user)

但我想使用 djangotables2 将返回的数据呈现为表,如下所示:

 import django_tables2 as tables

 class Job_table(tables.Table):
    class Meta: 
    model = Timer
    attrs = {"class": "paleblue"}
    fields= ('start_hour','end_hour','category','subcategory','duration')

    def render_duration(self,value):
        from timehuman import sectohour
        hh= sectohour(value)
        return hh

我如何将我的数据呈现为表格而不是呈现的列表?( django 的 context object_list ) 我如何访问要发送到 object_list 上下文的数据并修改它?

4

1 回答 1

0

这就是我最终做的事情:可能是hackish,但我让我可以访问将要进入视图的相同数据,并且我可以将其呈现为表格。

class day_archive(DayArchiveView):
    model=Timer
    paginate_by=12
    allow_future =True
    allow_empty=True
    month_format = '%m'
    day_format='%d'
    date_field='begin_time'
    template_name='timer/timer_archive_date'

    def get_queryset(self):
        return Timer.objects.filter(author=self.request.user)

    def render_to_response(self, context, **response_kwargs):
        """
        Returns a response, using the `response_class` for this
        view, with a template rendered with the given context.

        If any keyword arguments are provided, they will be
        passed to the constructor of the response class.
        """

    tbl = context['object_list']  #this line is my hack, i dont know better.
    if (tb1 != None):
        jt = Timer_table(blo)
        RequestConfig(self.request).configure(jt)
        from django.db.models import Sum
        total = tbl.aggregate(Sum('duration'))
        t2 = total['duration__sum']
        if (t2 != None):
            timedel= str(datetime.timedelta(seconds=float(t2)))
            context['table']= jt
            context['total'] = timedel 

    return self.response_class(
        request = self.request,
        template = self.get_template_names(),
        context = context,
        **response_kwargs
    )
于 2013-02-10T16:38:11.317 回答