在django时域中将输入设置为AM PM [英] set input to AM PM in django timefield

查看:182
本文介绍了在django时域中将输入设置为AM PM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户能够在我和django时区之间选择am和pm之间



目前,如果我输入:


11:00 AM


,我收到一个表单错误:输入有效时间。



如果我输入:


p>

表单验证没有问题。



我也尝试过:

  class RemindersForm(forms.ModelForm):

remdinder = forms.TimeField(input_formats ='%H:% M $ p',)

class Meta:
model = NotificationPreference
fields =(
'reminder',

将输入格式更改为:


11:00:00


仍然给我上述验证错误。



我做错了什么?

解决方案

要获得12小时时间格式,您将使用以下内容:



输入(这是格式列表Django用于验证):

  field = TimeField(input_formats =('%I:%M%p',... ,...))

输出(这是Django将用于显示时间值的格式) :

  field = TimeField(widget = TimeInput(format ='%I:%M%p'))
%I表示12小时时钟格式,而%H表示24小时时钟格式。

b
$ b

此外,input_formats的默认列表可以在每个语言环境formats.py文件中找到。 Django将input_formats列表中的第一个格式用作时间字段的默认输出格式。


i want to allow users to be able to choose between am and pm with my django timefield

Currently if I enter:

11:00 AM

, i get a form error: "Enter a valid time."

If I enter:

11:00

the form validates with no problem.

I also tried:

class RemindersForm(forms.ModelForm):

    remdinder = forms.TimeField(input_formats='%H:%M %p',)

    class Meta:
        model = NotificationPreference
        fields = (
                  'reminder', 
                  )

This changes the input format to:

11:00:00

and still gives me the above validation error.

What am I doing wrong?

解决方案

To get a 12hr time format you would use the following:

For input (This is the list of formats Django uses in validation):

field = TimeField(input_formats=('%I:%M %p',...,...))

For output (This is the format Django will use to display time values):

field = TimeField(widget=TimeInput(format='%I:%M %p'))

The %I indicates a 12 hour clock format whereas the %H indicates a 24 hour clock format.

Additionally, the default list of input_formats can be found in each locales formats.py file. Django uses the first format in the input_formats list as the default output format for time fields.

这篇关于在django时域中将输入设置为AM PM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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