Code

Opened 3 years ago

Closed 6 months ago

#14658 closed Bug (needsinfo)

DateField initial does not honor locale, against documentation

Reported by: intgr 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

Attachments (0)

Change History (11)

comment:1 Changed 3 years ago by rasca

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

I can confirm this happens.

comment:2 Changed 3 years ago by jaddison

  • Severity set to Normal
  • Type set to Bug

comment:3 Changed 3 years ago by jedie

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

comment:4 Changed 20 months ago by nferrari

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 Changed 17 months ago by claudep

  • Resolution set to worksforme
  • Status changed from new to closed

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 Changed 6 months ago by pylot

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 Changed 6 months ago by pylot

  • Resolution worksforme deleted
  • Status changed from closed to new

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 Changed 6 months ago by anonymous

  • Version changed from 1.2 to 1.5

comment:9 Changed 6 months ago by pylot

  • Triage Stage changed from Accepted to Unreviewed

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 Changed 6 months ago by claudep

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

comment:11 Changed 6 months ago by claudep

  • Resolution set to needsinfo
  • Status changed from new to closed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.