日期字段&使用 jQuery DatePicker 和 TimeField 进行格式化时间选择器
我有一个表单(2 个字段),用户可以在其中选择/输入日期和日期。使用 jQuery 的 DatePicker 和 Time时间选择器。出来的格式是:
01/05/2011
11:00 PM
表单不会提交,所以我认为这些格式和 DateField & 存在冲突。我在表单类中使用的 TimeField。这是真的吗?如果是这样,这些输入需要采用什么格式?
谢谢!
I have a form (2 fields) where the user selects/inputs a Date & Time using jQuery's DatePicker & TimePicker. The format's that come out are :
01/05/2011
11:00 PM
The form will not submit, so I think there is a conflict with those formats and the DateField & TimeField I am using in my form class. Is this true? If so, what format do these inputs need to be?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过查看 Django 源代码来阅读这些字段的验证代码。
查看 django/forms/fields.py,阅读 DateField 和 TimeField 类上的 to_python() 方法。
在我看来,它们都支持多种输入类型:
我认为最后一个选项就是您想要的想。查看django/conf/global_settings.py,可以看到这些设置的默认值,其中包括大量的strptime格式字符串。这是默认的 DATE_INPUT_FORMATS:
因此,任何采用这些方式格式化的日期字符串都应该可以工作(假设您的语言环境没有设置为古怪的东西——每个语言环境都有该字段的语言环境特定覆盖,并且您应该检查语言环境子目录中的您正在使用的区域设置)。
TIME_INPUT_FORMATS 也有类似的逻辑,默认情况下是:
所以我怀疑这就是阻止您的表单工作的原因,因为您的时间有 AM/PM。
您可以在 settings.py 中手动将 TIME_INPUT_FORMATS 设置为 strptime 格式字符串,该字符串将适合您想要的内容,这可能类似于“%I:%M %p”(请注意,您不能只使用 %r 因为看起来需要几秒钟)。您可能还想包含原始值,只是为了与其他事物(例如 Django 管理界面)兼容。
我不太熟悉 jQuery date & 。时间选择器——也许您可以将它们配置为以 Django 已经支持的格式输出时间?
You can read the validation code for these fields by looking at the Django source.
Looking in django/forms/fields.py, read the to_python() methods on the DateField and TimeField classes.
It looks to me like they both support a number of input types:
I think the last option is what you want. Checking in django/conf/global_settings.py, you can see the default values of these settings, which include a large range of strptime format strings. Here's the default DATE_INPUT_FORMATS:
So, any date string formatted any of these ways should work (assuming your locale isn't set to something wacky -- each locale has locale-specific overrides for this field, and you should check the locale subdirectory for the locale you're working with).
There's similar logic for TIME_INPUT_FORMATS which by default are:
So I suspect that's what's preventing your form from working, since your time has AM/PM.
You can set TIME_INPUT_FORMATS manually in your settings.py to a strptime format string that will work for what you want, which will probably be something like "%I:%M %p" (Note that you can't just use %r because it looks like it requires seconds). You might want to include the original values as well, just for possible compatibility with other things (like the Django admin interface).
I'm not that familiar with the jQuery date & time pickers -- maybe you can configure them to output the time in a format that's already supported by Django?
我的设置运行良好,只需
在我的 settings.py 中 使用
并使用 jQueryUI 的语言文件
此页面有 jqueryUI 语言文件的完整示例
I have this setup running fine just using
in my settings.py
and using a language file for jQueryUI
this page has the full example of a jqueryUI language file