Learning Django
Learning Django

Reputation: 265

django default date to be in format %d-%m-%Y

I want default date but in '%d-%m-%Y' format.

models.py

date = models.DateField(default=datetime.date.today)

what i have tried

forms.py

date = forms.DateField(input_formats='%d-%m-%Y')

Trying the above is not giving any default date in the form.

Upvotes: 1

Views: 10119

Answers (3)

Marcell Erasmus
Marcell Erasmus

Reputation: 914

Your input formats should be a list:

date = forms.DateField(input_formats=['%d-%m-%Y'])

You can read more about this here

Upvotes: 0

ans2human
ans2human

Reputation: 2357

Use Format method in your forms.py:

date = forms.DateField(widget=DateInput(format='%d-%m-%Y'), 
                       input_formats=['%d-%m-%Y'])

Upvotes: 0

Sushant
Sushant

Reputation: 3669

If you want to change default date format site wide, look at -

DATETIME_FORMAT

And in template, you can use a filter -

{{you_date_field|date:'%d-%m-%Y'}}

Upvotes: 3

Related Questions