我在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上下文并修改它的数据?
答案 0 :(得分: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
)