python - Django 时间字段格式

标签 python django time format models

我的时间格式如下:“上午 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/

相关文章:

python - Wing IDE 是否有 Eclipse 个性设置?

javascript - Django CMS/WYMEditor 停止剥离空白

python - Django 模型中的 request.user

django - 错误: client intended to send too large body

c# - 确定数组是否包含重复值的最快方法是什么?

c - time_t 的转换如何在 C 中工作?

python - Pycharm:只运行我的 Python 文件的一部分

django - 嵌套网址不适用于 Django REST 框架

C++ 随机函数不是随机的

python Tornado websocket错误: RuntimeError: There is no current event loop in thread 'Thread-1'