3

In a Django template I'm displaying a date like:

{{ article.published_at|date }}

which displays like:

Nov. 21, 2018

I would like to change this format, but:

  • I am using Localization, so I don't want to specify a specific date format in the template -- each language will have its own way of displaying dates.

  • The Django DATE_FORMAT setting does not have any effect when the L10N setting is True.

So how would I:

  1. Use a different format for this date when the current language is English (en)
  2. Use the current language's default date format when it's not en

2 Answers 2

1

This is the filter I've ended up using as a replacement for the built-in date filter.

# myapp/templatetags/my_filters.py
from django import template
from django.conf import settings
from django.template.defaultfilters import date
from django.utils import dateformat
from django.utils.translation import get_language

register = template.Library()

@register.filter(expects_localtime=True, is_safe=False)
def format_date(value):
    if value in (None, ''):
        return ''

    language_code = get_language()

    if language_code == 'en':
        # Language is English, so use the format defined in settings:
        return dateformat.format(value, settings.DATE_FORMAT)
    else:
        # Language is not English, so use language-specific default:
        return date(value)

I use the normal DATE_FORMAT Django setting in settings.py to set a custom date format:

DATE_FORMAT = 'j F Y'

This is used if the current language is English. Otherwise the current language's default is used.

Use it like:

{% load my_filters %}

{{ article.published_at|format_date }}

You could extend this to provide custom formats for additional languages, but this is all I need for now.

I still expected there to be a way to define per-language date formats in settings, so I still feel like I've missed a simpler way of doing this...

1

Does your template include the {% load l10n %} tag? You can specify the date format already using the tag above and

for example if you were looking for the date to be displayed like: 1/13/2020

you wourl do: your_date|date:'n/j/Y'

see Link to the docs for builtins |date

Never much point rolling your own when it already exists, would recommend however considering adding a date_format="n/j/Y" in the context. Then you can just right:

{{ item.date_end|date:date_format }}

Then you wont have the format specified in the template, plus you can if it's used everywhere create a context processor to define it so it's included in every render. Thus making it super easy to change site wide.

Create your own context processor

6
  • It didn't. I've tried adding it but that doesn't do anything different on its own. Jan 7, 2019 at 16:58
  • Try specifying LANGUAGE_CODE (remember it also needs USE_I18N = True). If you want your app to dynamically determine language preference, look into LocaleMiddleware.
    – mgradowski
    Jan 7, 2019 at 18:00
  • Sorry, I don’t know what you mean. The entire site is working fine in multiple languages, with translations, and the user can switch between language versions. Jan 7, 2019 at 21:08
  • Oh, okay then. I misunderstood your question and somehow assumed that you have problems setting up localization in a new application.You can specify how the date will be displayed in the template like so {{ value|date:"D d M Y" }} (check docs). If you want to customize this for every language you can pass django.utils.translation.get_language() to template context and use a simple conditional block. Is this what you had an issue with?
    – mgradowski
    Jan 7, 2019 at 21:55
  • I don’t know how to describe the problem any better than I have in points 1 and 2 in the question. I don’t want to use a conditional in the template every time I display a date. I guess I’ll have to write my own template filter, but I thought there might be something simpler that I’d missed. Jan 8, 2019 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.