【问题标题】:The specified value does not conform to the required format指定的值不符合要求的格式
【发布时间】:2021-12-27 07:32:31
【问题描述】:

我在django 模板中有这个代码sn-p

<td><input type="datetime-local" id="tdId_{{i.0}}5" value="{{i.4|date:'Y-m-d H:i'}}"/></td>

控制台写道 The specified value ... does not conform to the required format. ...

我应该使用哪个掩码来匹配datetime-local 的所需格式?

【问题讨论】:

  • 也许你这里没有日期时间变量i.4
  • @Eugenij i.4 是一个日期时间变量。

标签: django django-templates


【解决方案1】:

datetime-local 接受ISO 8601 日期格式。根据documentation,您可以使用格式字符c 将日期时间对象转换为ISO 8601 format

<input type="datetime-local" id="tdId_{{i.0}}5" value="{{i.4|date:'c'}}"/>

【讨论】:

  • 是否可以在小部件中隐藏微秒?
【解决方案2】:

此解决方案也有效:

{{ i.4|date:'Y-m-d'}}T{{ i.4|time:'H:i:s' }}

所以,整个代码是:

<input type="datetime-local" id="tdId_{{i.0}}5" value="{{ i.4|date:'Y-m-d'}}T{{ i.4|time:'H:i:s' }}"/>

【讨论】:

    猜你喜欢
    • 2019-01-11
    • 2015-08-28
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多