1

I have a model with DateField, set like this :

date = models.DateField(blank=False, default=datetime.now)

each time I put that field data in a tamplate ({{obj.date}}) it shown in this format :

July 24, 2014

and I want to change that permanently to this format:

24.7.2014

also I have a search page where data can be searched by its date field - I want to be able to search in this format as well. How can I do that?

EDIT: I think it has somthing to do with the LANGUAGE_CODE = 'en-us' setting. when I change that it also changes the format of the date. how can it be overwrited?

user2216190
  • 784
  • 5
  • 10
  • 25

3 Answers3

2

Django is using l10n to format numbers, dates etc. to local values. Changing your LANGUAGE_CODE is a good point and allows Django to load the correct environment.

In addition to this you need to configure the use of l10n via USE_L10N=True See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-USE_L10N

To enable a form field to localize input and output data simply use its localize argument:

class CashRegisterForm(forms.Form):
   product = forms.CharField()
   revenue = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)

This works also fine for dates.

(from: https://docs.djangoproject.com/en/dev/topics/i18n/formatting/#locale-aware-input-in-forms)

edit: For the use in template you can just simple format dates via:

{% load l10n %}

{{ value|localize }}

or

{{ your_date|date:"SHORT_DATE_FORMAT" }}

second one is using the SHORT_DATE_FORMAT in settings.py

cheers, Marc

mrcrgl
  • 640
  • 4
  • 11
  • it still not the solution im looking for - what i want to do is to overwrite the format being used for everything. i want to be able to search the database by this format, and to display the data in the same format. forms are not realated to the question. Thanks! – user2216190 Aug 08 '14 at 14:17
  • changing the languge-code is possible but the languge code in our country is not the one that im looking for. – user2216190 Aug 08 '14 at 14:18
  • 1
    seriously, the way you put data into your code is via forms. There you're sure that the correct format is used and that there is no malware code in. This should be the way: [Browser/UserInput] -> [Form] -> [View] -> [Model] – mrcrgl Aug 12 '14 at 09:16
1

use pynthon datetime:

from datetime import datetime

date = models.DateField(blank=False, default=datetime.now().strftime("%d.%m.%Y"))
slim_chebbi
  • 798
  • 6
  • 9
  • I dont know how it actually saved on the data base, but when I look at the database with databse browser, the date field data shown in this format: `2014-07-17`, so i think that it has nothing to do with the saving format, but with the way the data is presented. – user2216190 Aug 08 '14 at 13:30
  • thats why im looking for a solution to change the way django shows that kind of data by default – user2216190 Aug 08 '14 at 13:31
1

If what you are concerned about is how the date is displayed (output format); you can use a date filter, as explained here:

{{ obj.date|date:"d.m.Y"}} 

And here the Django docs.

Community
  • 1
  • 1
J0ANMM
  • 7,849
  • 10
  • 56
  • 90