others - 如何让Django forms.DateField 显示本地日期格式


from models import *


from django import forms


import datetime



class MyDateWidget(forms.TextInput):



 def render(self, name, value, attrs=None):



 if isinstance(value, datetime.date):


 value=value.strftime("%d/%m/%Y")



 return super(MyDateWidget, self).render(name, value, attrs)



class MyDateField(forms.DateField):



 widget = MyDateWidget



 def __init__(self, *args, **kwargs):


 super(MyDateField, self).__init__(*args, **kwargs)


 self.input_formats = ("%d/%m/%Y",)+(self.input_formats)



class ExampleForm(ModelForm):


 class Meta: 


 model=MyModel


 fields=('name', 'date_of_birth')



 date_of_birth = MyDateField()



时间:

我不认为你的方法有什么问题,

只需对DateInput小部件使用format参数,应该就可以了。在你的MyDateField类中:


class MyDateField(forms.DateField):



 widget = forms.DateInput(format="%d/%m/%Y")



 def __init__(self, *args, **kwargs):


 super(MyDateField, self).__init__(*args, **kwargs)


 self.input_formats = ("%d/%m/%Y",)+(self.input_formats)



你可以使用formfield_callback:


def customize_fields(f):


 if isinstance(f, DateTimeField):


 datetime_field=forms.DateTimeField(widget=


 forms.DateInput(format='%d/%m/%Y %h:%i'))


 date_field.input_formats = ("%d/%m/%Y %h:%i",)+


 (date_field.input_formats)


 return datetime_field


 elif isinstance(f, DateField):


 date_field=forms.DateField(widget=


 forms.DateInput(format='%d/%m/%Y'))


 date_field.input_formats = ("%d/%m/%Y",)+


 (date_field.input_formats)


 return date_field


 else:


 return f.formfield()



class MyModelForm(forms.ModelForm):


 formfield_callback = customize_fields



 class Meta:


 model = ...



formfield_callback=customize_fields

如果你需要在ModelForm中重新定义模型字段,说明使该字段不需要该表单的特定实例,如下所示:


 class MyModelForm(forms.ModelForm):


 formfield_callback = customize_fields


 foo = forms.DateField(required=false)



 class Meta:


 model = Bar



我以前使用过这种简单的方法:只需在Form的初始化中设置DateField.widget的'format '属性:


 class ExampleForm(ModelForm):



 class Meta:


 model = MyModel



 def __init__(self, *args, **kwargs):


 super(ModelForm, self).__init__(*args, **kwargs)


 self.fields['date_of_birth'].widget.format = '%d/%m/%Y'



 # at the same time, set the input format on the date field like you want it:


 self.fields['date_of_birth'].input_formats = ['%d/%m/%Y']



所有自定义部件都可以跳过。

以下是django/forms/widgets.py的摘录:


class DateInput(TextInput):


 def __init__(self, attrs=None, format=None):


 super(DateInput, self).__init__(attrs)


 if format:


 self.format = format


 self.manual_format = True


 else:


 self.format = formats.get_format('DATE_INPUT_FORMATS')[0]


 self.manual_format = False



您会注意到,小部件的格式是使用format模块设置的。根据区域设置选择格式模块,django.conf.formats.locale.en.formats.py在这里,你会找到默认格式:


DATE_INPUT_FORMATS = (


 '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'


 # '%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006'


 # '%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006'


 # '%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006'


 # '%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006'


)



看看这个FORMAT_MODULE_PATH,我将它添加到设置文件中,所有内容都非常适合首选默认格式:


DATE_INPUT_FORMATS = ('%m/%d/%Y', '%m/%d/%y', '%Y-%m-%d',


 '%b %d %Y', '%b %d, %Y', '%d %b %Y',


 '%d %b, %Y', '%B %d %Y', '%B %d, %Y',


 '%d %B %Y', '%d %B, %Y')


from django.conf.locale.en import formats


formats.DATE_INPUT_FORMATS = DATE_INPUT_FORMATS



在表单中,我使用:


def today():


 from datetime import date


 return date.today().strftime("%d.%m.%Y")



when = models.DateField(u'Когда', default=today)



...