Sco*_*ton 4 html python django django-tables2
我当前正在使用django-tables2显示我的模型的查询集。该模型的属性之一是精确到毫秒的DateTimeField,该时间被截断到表中的分钟。
我以前用HTML手动实现了一个简单的表,没有任何问题。我的DateTimeFields遵循我的设置中应用的DATETIME_FORMAT:
settings.py
DATETIME_FORMAT = 'Y N j, H:i:s.u'
Run Code Online (Sandbox Code Playgroud)
自从我开始使用django-tables2以来,就出现了问题。有什么方法可以修改其显示DateTimeFields的方式或使其遵循我指定的DATETIME_FORMAT?我需要保留排序功能,因此无法转换为字符串。
我正在使用render_table显示我的表。以下是我的表类:
class ModelTable(tables.Table):
class Meta:
model = Measurement
sequence = ('date_time', 'latitude', 'longitude',
'depth', 'soundvel', 'instrument')
Run Code Online (Sandbox Code Playgroud)
问题解决了。
django-table2的DateTimeColumn类似乎正在我的settings.py中寻找SHORT_DATETIME_FORMAT而不是DATETIME_FORMAT。更新了我的设置文件中的值,一切正常。
当我尝试使用 Python 日期时间格式选项时,这让我困惑了一段时间。Django 模板的格式选项适用于 django-tables2,并在 Django 文档中完整列举:https ://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatefilter-date
由此,如果您有一个带有一个日期时间列的模型,并且您希望将他们的生日格式化为Month Day Year, Hour:Minute AM/PM,那么您将输入以下内容:
class MyTable(tables.Table):
birthday = tables.DateTimeColumn(format ='M d Y, h:i A')
class Meta:
model = Person
attrs = {'class': 'table'}
fields = ['birthday']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2248 次 |
| 最近记录: |