Avatar
💡
1 results for Django
  • Here is a simple django template filter that will format a date according to the DATETIME_FORMAT variable in the settings file.

    from django import template
    from settings import DATETIME_FORMAT
    from django.template.defaultfilters import date
    
    register = template.Library()
    
    @register.filter
    def default_datetime(value):
        try:
            v = date(value, DATETIME_FORMAT)
        except:
            return value
    
        return v
    

    Save this in a file named default_date_filters.py and place it in a directory called templatetags in your application directory (along with a file named init.py.

    You can then use the filter in a template like so:

    django Created Fri, 02 May 2008 12:32:01 +0000