Opened 14 years ago

Closed 11 years ago

#14658 closed Bug (needsinfo)

DateField initial does not honor locale, against documentation

Reported by: Marti Raudsepp Owned by: nobody
Component: Forms Version: 1.5
Severity: Normal Keywords:
Cc: django@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The documentation at http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.initial suggests that when passing the intial= argument to DateField, it's converted to the locale date format:

>>> import datetime
>>> class DateForm(forms.Form):
...     day = forms.DateField(initial=datetime.date.today)
>>> print DateForm()
<tr><th>Day:</th><td><input type="text" name="day" value="12/23/2008" /><td></tr>

Notice that value="12/23/2008" is localized.

However, this does not seem to work in practice:

In [1]: from django import forms

In [2]: import datetime

In [3]: class DateForm(forms.Form):
   ...:     day = forms.DateField(initial=datetime.date.today)

In [4]: print DateForm()
<tr><th><label for="id_day">Day:</label></th><td><input type="text" name="day" value="2010-11-10" id="id_day" /></td></tr>

Notice above value="2010-11-10" -- it behaves this way in both Django 1.2.3 and in trunk.

Compare this to what the templater uses with its |date filter:

In [10]: from django.template import Template, Context

In [11]: t=Template("{{ foo|date }}")

In [12]: t.render(Context({'foo': datetime.date.today()}))
Out[12]: u'10.11.2010'

In [13]: from django.conf import settings

In [14]: settings.DATE_FORMAT
Out[14]: 'd.m.Y'

In [15]: settings.DATETIME_FORMAT
Out[15]: 'd.m.Y H:M:s'

In [16]: settings.USE_L10N
Out[16]: True

Change History (11)

comment:1 by rasca, 14 years ago

Triage Stage: UnreviewedAccepted

I can confirm this happens.

comment:2 by James Addison, 14 years ago

Severity: Normal
Type: Bug

comment:3 by jedie, 14 years ago

Cc: django@… added
Easy pickings: unset
UI/UX: unset

comment:4 by Nicolas Ferrari, 12 years ago

Hi,

I didn't take time to patch Django yet but here's the trick to have a forms.DateField localized initial value:

Code highlighting:

date = forms.DateField(initial=lambda: django.utils.formats.localize_input(datetime.date.today()))

Regards,

comment:5 by Claude Paroz, 12 years ago

Resolution: worksforme
Status: newclosed

Just ran this snippet with current 1.5 code:

import datetime
from django import forms
from django.utils.translation import activate

activate('fr')

class DateForm(forms.Form):
    day = forms.DateField(initial=datetime.date.today)

print(DateForm())

Result:

<tr><th><label for="id_day">Day:</label></th><td><input type="text" name="day" value="01/12/2012" id="id_day" /></td></tr>

comment:6 by pylot, 11 years ago

import datetime
from django import forms
from django.utils.translation import activate, deactivate

activate('de')

class deDateForm(forms.Form):
    date = forms.DateTimeField()
    def __init__(self, *args, **kwargs):
        super(self.__class__, self).__init__(*args, **kwargs)
        self.fields['date'].initial = datetime.datetime.now

class enDateForm(forms.Form):
    date = forms.DateTimeField()
    def __init__(self, *args, **kwargs):
        super(self.__class__, self).__init__(*args, **kwargs)
        self.fields['date'].initial = datetime.datetime.now


e = enDateForm()

deactivate()
activate('de')

d = deDateForm()
print e
print '-----------------------------------------------------'
print d

Result:

<tr><th><label for="id_date">Date:</label></th><td><input id="id_date" name="date" type="text" value="13.10.2013 22:01:02" /></td></tr>
<tr><th><label for="id_date">Date:</label></th><td><input id="id_date" name="date" type="text" value="13.10.2013 22:01:02" /></td></tr>

comment:7 by pylot, 11 years ago

Resolution: worksforme
Status: closednew

line 5 should be 'en'

anyway, output is 2x:

<tr><th><label for="id_date">Date:</label></th><td><input id="id_date" name="date" type="text" value="2013-10-13 22:03:15" /></td></tr>

seems to be fetched only once the server starts... suspicous

comment:8 by anonymous, 11 years ago

Version: 1.21.5

comment:9 by pylot, 11 years ago

Triage Stage: AcceptedUnreviewed

I did some tests... it seems like it has to do something with file caching ... so testing with one file leads to a working solution, but this is not what is done in practice, especially with model forms

first in file test_app.models define the model to be tested:

import datetime

class Material(models.Model):
        date = models.DateTimeField(default=datetime.datetime.now, blank=True)

in test_app.forms define the form which is not working as suspected

from django import forms
from test_app.models import Material

class TestForm(forms.ModelForm):
        class Meta:
                model = Material

then i created a view to test it in a live environment to compare:

  • a ModelForm which is created (and working) in this file
  • a ModelForm which is imported

both work with the imported Model, so there should be no difference

def test_date(request):
        from django import forms
        from django.shortcuts import render_to_response
        from test_app.models import Material
        from test_app.forms import TestForm
       
        class MaterialTestForm(forms.ModelForm):
                class Meta:
                        model = Material

        import_form = TestForm()
        direct_form = MaterialTestForm()

        return render_to_response("test_date.html", locals(), RequestContext(request))

in the template compare the output of {{import_form}} and {{direct_form}}, first no differnce, then switch languages, and you see the import_form is not updated and still displaying the refreshed date with the locale first initialized...

comment:10 by Claude Paroz, 11 years ago

Could you please test with master? I just fixed a related bug in #21173.

comment:11 by Claude Paroz, 11 years ago

Resolution: needsinfo
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top