我在项目中实现了django-tables2以更快地加载数据。我mytags.py
包含许多自定义模板标记。有些人可以帮助我如何将我的标签实现到我的HTML中?以下是我的一个标签的示例,以及我在执行django-tables2之前如何在我的html中实现。提前致谢。
html:
<td>{{ table.start_time|get_total:table.Date_Time_End|default:"---" }}</td>
标签:
@register.filter
def get_total(date_start=None, date_end=None):
fmt = '%Y-%m-%d %H:%M:%S'
if date_start is not None and date_end is not None:
ds = str(date_start)
new_ds = ds[:19]
de = str(date_end)
new_de = de[:19]
date_start = datetime.strptime(new_ds, fmt)
date_end = datetime.strptime(new_de, fmt)
return date_end - date_start
else:
return None
答案 0 :(得分:1)
如果您想在表格中添加其他字段 - 请尝试使用
class YourModel(models.Model):
# here your fields
@property
def total_date(self):
fmt = '%Y-%m-%d %H:%M:%S'
if self.date_start is not None and self.date_end is not None:
ds = str(self.date_start)
new_ds = ds[:19]
de = str(self.date_end)
new_de = de[:19]
date_start = datetime.strptime(new_ds, fmt)
date_end = datetime.strptime(new_de, fmt)
return date_end - date_start
else:
return None
然后将其添加到您的表类字段
class YourTableClass(tables.Table):
total_date = tables.Column(verbose_name='Total date')
class Meta:
model = YourModel