我的时间格式如下:“上午 10 点,中午”。我想要的格式为:10:00、12:00、15:00 等。 我在我的 html 文件中尝试过:
{{ value|time:"H:M" }}
我也在我的设置中尝试过
TIME_INPUT_FORMATS = ('%H:%M',)
没有任何作用。
在我的模型中,我有:
time = models.TimeField(blank=True, null=True)
views.py
times= Hours_classes.objects.all()
context = {'teacher_id': teacher_id,
'query_results': query_results,
'times': times}
return render(request, 'planner/teacher.html', context)
教师.html
<tr>
<th></th>
{% for item in times %}
<th>{{ item.time_start|time:"H:M" }} - {{ item.time_end|time:"H:M" }} </th>
{% endfor %}
</tr>
models.py
class Hours_classes(models.Model):
time_start = models.TimeField(blank=True, null=True)
time_end = models.TimeField(blank=True, null=True)
最佳答案
“H:M”
不是传递给 Django template time filter 的有效格式。 。您需要使用:
{{ value|time:"H:i" }}
关于python - Django 时间字段格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34841008/