Code

Opened 20 months ago

Closed 20 months ago

Last modified 20 months ago

#18841 closed Bug (invalid)

djnago admin date fileds messed up when using DATE_INPUT_FORMATS = ('%j. %n. %Y', '%j. %n. %y')

Reported by: clay.evil@… Owned by: nobody
Component: contrib.admin Version: 1.4
Severity: Normal Keywords: DATE_INPUT_FORMATS admin
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

when i set DATE_INPUT_FORMATS = ('%j. %n. %Y', '%j. %n. %Y',) for my project, it seems that django admin somehow cannot display date fields properly and also the javascript calendar is not working. "j" = days without leading zeros, and "n" = months without leading zeros.

is there an easy way to disable i18n in django admin until this bug is resolved?

Thank you.

Attachments (0)

Change History (5)

comment:1 Changed 20 months ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

The documentation explains that DATE_INPUT_FORMATS format strings use Python's datetime module syntax.
http://docs.python.org/library/datetime.html#strftime-strptime-behavior

%j Day of the year as a decimal number [001,366].
%n is not mentioned.

comment:2 Changed 20 months ago by kmtracey

The problem is j and n don't mean what is stated here, in the context where they are trying to be used. The doc for DATE_INPUT_FORMATS (https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DATE_INPUT_FORMATS) states:

Note that these format strings use Python's datetime module syntax, not the format strings from the date Django template tag.

That links to the Python datetime module syntax: http://docs.python.org/library/datetime.html#strftime-strptime-behavior where j is " Day of the year as a decimal number [001,366]." an n has no meaning.

The meanings stated appear to come from https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatefilter-date. As noted above those output formatting strings are not what's used for the input format specifications. Yes, this is a bit weird and inconsistent but there's not much that can be done about it now.

comment:3 Changed 20 months ago by anonymous

First, my apologies, you're both right, the docs states it uses strftime.

But i have another problem with this.
Django displays the format wrong, but it is albe parse it very well. If i enter 1. 1. 2012 in the filed, it gets parsed. So its seems that django uses somethign else to display and to parse the input.

The problem is, i strftime according to doc has not any letter for days or month without leasing zeros, but i did google that if you put "-" before the format letters, its strips the leading zeros. i.e. "-d. -m. Y".
This works well sice the input filed is rendered properly without leading zeros BUT it cannot parse the input on submit.

So to summarize:

if i use '%j. %n. %Y' the input gets parsed well, but is rendered incorrectly

if i use "-d. -m. Y". the inputs renders correctly, but gets not parsed.

---

I know that "-d. -m. Y" is not documented in python. But is there a way to get working at least one of the format string?

---

Also, in my project i didnt notice this issue as i use custom fileds and widgets in my projects

class HumanTextInput(TextInput):

def _format_value(self, value):

logger.debug("_format_value:%s", value)
if value == date.today():

return ugettext("cal_today")

elif value == (date.today() + timedelta(days=1)):

return ugettext("cal_tomorrow")

return super(HumanTextInput, self)._format_value(value)

class HumanDateFiled(forms.DateField):

def to_python(self, value):

if value == ugettext("cal_today"):

return date.today()

elif value == ugettext("cal_tomorrow"):

return date.today() + timedelta(days=1)

return super(HumanDateFiled, self).to_python(value)

class NewsModuleArticleForm(InstawebModelForm):

pub_date = HumanDateFiled(label=_("field_article_publish_date"), widget=HumanTextInput, initial=date.today)

This way a user can type "today" or "tomorrow" in the input fields. a small feature in out project. However, this way, i can use the '%j. %n. %Y' format string in DATE_INPUT_FORMATS and everything works well.. except django admin..

comment:4 Changed 20 months ago by anonymous

sorry, using my code above django formats the input as "Y-%m-%d" i.e. 2012-08-17, i don't know why yet. but i don't want to miss lead you. i just found out...

comment:5 Changed 20 months ago by clay.evil@…

ok, sorry don't waset your time, i did find a solution, all my fault. sorry to bothered thou.

if anyone is intereds, this strings are working for me:
DATE_INPUT_FORMATS = (

'%-d. %-m. %Y', '%-d. %-m. %y',
'%-d.%-m.%Y', '%-d.%-m.%y',
'%Y-%-d-%-m', '%y-%-d-%-m',
'%-d/%-m/%Y', '%-d/%-m/%y',
'%d. %m. %Y','%d. %m. %y',
'%d.%m.%Y', '%d.%m.%y',
'%Y-%m-%d', '%y-%d-%m',
'%d/%m/%Y', '%d/%m/%y',
'%d %b %Y', '%d %b, %Y', '%B %d %Y',
'%B %d, %Y', '%d %B %Y', '%d %B, %Y')

+ i did corrent a mystake in my code in the HumanTextInput, which sloud be named HumanDateInput and inherit from forms.DateInput, like this:

class HumanDateInput(forms.DateInput):

def _format_value(self, value):

logger.debug("_format_value:%s", value)
if value == date.today():

return ugettext("cal_today")

elif value == (date.today() + timedelta(days=1)):

return ugettext("cal_tomorrow")

return super(HumanTextInput, self)._format_value(value)

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.