Django:如何格式化DateField的日期表示? [英] Django: How to format a DateField's date representation?

查看:1401
本文介绍了Django:如何格式化DateField的日期表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DateField的表单。现有的日期值格式如下:



2009-10-03



如何格式化,使其如下所示:



03.10.2009



我发现了一个这样的窗口小部件,但验证不允许我输入的值。

解决方案

要显示正确格式的初始字段值,请使用 DateInput 小部件。要自定义验证,请使用 input_formats 关键字参数 DateField



为什么你需要格式 input_formats



格式 - 将显示此字段的初始值的格式。请参阅 here



input_formats - 用于尝试将字符串转换为有效的 datetime.date 对象的格式列表。请参阅此处


I have a form with a DateField. The existing date value is formatted to render like this:

2009-10-03

How can I format that so that it look like this:

03.10.2009

I found a widget which renders it like this, but validation doesn't allow the values I enter then.

解决方案

To display initial field value properly formatted, use DateInput widget. To customize validation, use input_formats keyword argument of DateField.

Why do you need both format and input_formats?

format - The format in which this field’s initial value will be displayed. See here.

input_formats - A list of formats used to attempt to convert a string to a valid datetime.date object. See here.

这篇关于Django:如何格式化DateField的日期表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆