0

I am working on an app that should support different languages and flavours such as American, British and German.

Now looking at the documentation, e.g. there seem to be no direct support for American date format. It seems this part needs to be done by hand.

So I kept digging into the documentation and found this:

{{ value|date:"D d M Y" }}

This doesn't seem to work well with forms though, when I tried this in my code:

{{form.birth_date|date:"D d M Y"}}

It seems the formatting needs to be set right at the form field itself.

More research showed a similar question was asked in Stackoverflow in 2009:

This solution could work, but it looks quite engaging for such a simple problem. Django has come a long way since 2009, so I wonder if there is a better way of doing it right into ModelForm?

Let say we have the following code:

Model:

class ConversationHistory(models.Model):
    contact_date        = models.DateField(_(u"Conversation Date"))

Form:

class ConversationForm(ModelForm):        
    class Meta:
        model = ConversationHistory

Could I just add the following to ModelForm:

widgets = { 'contact_date': forms.DateInput(format='M d, Y') }

is this the right way of localization? What if I wanted to change the format to Brtish English or German? So I have the feeling I am missing here something...

Many Thanks,

UPDATE:

I have now tried this:

widgets = { 'contact_date': forms.DateInput(format='SHORT_DATE_FORMAT') }

But the date shows as SHORT_DATE_FORMAT.

What am I missing?

Community
  • 1
  • 1
Houman
  • 64,245
  • 87
  • 278
  • 460
  • Maybe [SHORT_DATE_FORMAT](https://docs.djangoproject.com/en/dev/ref/settings/#std%3asetting-SHORT_DATE_FORMAT) – jpic Jul 11 '12 at 09:41

2 Answers2

0

You're looking for the x format. Or worst case you just I18Nize the format and let the translator figure it out.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • but when I leave out the format altogether why am I seeing 2012/07/11 instead of American date format? In settings I have set the language to `LANGUAGE_CODE = 'en-us'`. The whole thing doesn't make sense to me. – Houman Jul 11 '12 at 17:05
0

I have been working on a solution for this with dojo for 2YRS!!! And I finally manged to find the the following works if you are using the dojango intergration.

someDate = DateField(widget=DateInput(attrs={'constraints':'{datePattern:\'dd/MM/yyyy\', strict:true}'))

forms.py

I don't know if that helps

Robert Johnstone
  • 5,431
  • 12
  • 58
  • 88