Re: Can't get date formatting right
On 27/06/2012 10:42, Benedict Verheyen wrote:
> Hi,
>
> I'm still struggling with getting dates formatted correctly without formatting them myself.
> I went over all docs again, still, I must be doing something wrong.
>
> I want to use the built in Django system that formats the date according to the locale.
> I try this on a Debian Linux Squeeze, locale output is this:
> # locale
> LANG=nl_BE.UTF-8
> LANGUAGE=
> LC_CTYPE="nl_BE.UTF-8"
> LC_NUMERIC="nl_BE.UTF-8"
> LC_TIME="nl_BE.UTF-8"
> LC_COLLATE="nl_BE.UTF-8"
> LC_MONETARY="nl_BE.UTF-8"
> LC_MESSAGES="nl_BE.UTF-8"
> LC_PAPER="nl_BE.UTF-8"
> LC_NAME="nl_BE.UTF-8"
> LC_ADDRESS="nl_BE.UTF-8"
> LC_TELEPHONE="nl_BE.UTF-8"
> LC_MEASUREMENT="nl_BE.UTF-8"
> LC_IDENTIFICATION="nl_BE.UTF-8"
> LC_ALL=
>
> I want to use Belgian/Dutch date formatting. I plan to use 3 languages for the time being yet
> the translation files aren't there yet.
> Next, my settings.py
> ...
> TIME_ZONE = 'Europe/Brussels'
> ugettext = lambda s: s
> LANGUAGES = (
> ('nl', ugettext('Dutch')),
> ('fr', ugettext('French')),
> ('en', ugettext('English')),
> )
> DEFAULT_LANGUAGE = 1
> LANGUAGE_CODE = "nl_BE"
> USE_I18N = True
> USE_L10N = True
> USE_TZ = True
> ...
>
> In my template, i load I18N and L10N
>
> {% load i18n %}
> {% load l10n %}
>
> and I even specifically say to localize the date in my template:
> {{form.day_of_birth|localize}}
>
> The form class used to display the info:
>
> class MyForm(forms.ModelForm):
> def __init__(self, *args, **kwargs):
> super(MyForm, self).__init__(*args, **kwargs)
> class Meta:
> model = MyTest
> fields = ('name', 'day_of_birth')
> # Added this line to try and force the localization
> day_of_birth = forms.DateTimeField(localize=True)
>
> Yet, I only see a date like this 2012-04-01 and not like 01/04/2011.
> Also, I can't enter a date like 01/04/2011 but it does accept 2012-04-01
>
> What am I doing wrong. I used to solve this in the past by filtering the date with a tag
> but I really want to do it right and make use of the built-in features.
>
> Can anybody shed some light on this please?
>
> Cheers,
> Benedict
>
I found a bug where there was a reference too translation.activate(settings.LANGUAGE_CODE)
I tried this as the first line of code in the view, and then my date is formatted correcly
and a date of format dd/mm/yy is accepted.
Is it necessary to add the translation line or is there another way?
Also, what if the language changes, will that line still work as it gets the language code
from the settings, and not the currently active code.
Cheers,
Benedict
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home