

Creating a default DATETIME_FORMAT filter for django
source link: http://www.mikechambers.com/blog/2008/05/02/creating-a-default-datetime_format-filter-for-django/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creating a default DATETIME_FORMAT filter for django
Friday, May 2, 2008
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:
{% load default_date_filters %}
{% item.created_date|default_datetime%}
You can find more information and how to create and use custom template filters in django here and here. You can find information on django settings here.
Recommend
-
10
Force to Use DATETIME Type in MySQL with Django Sep 29, 2020 TL;DR
-
9
How to filter by DateRange with django-filters Hi, I'm Ana. I'm a software developer. I mostly do Python. This blog is a...
-
8
Django-filter 0.5 released! · Alex Gaynor Alex Gaynor Hi, I'm Alex. I've been CISO a...
-
13
Announcing django-filter · Alex Gaynor Alex Gaynor Hi, I'm Alex. I've been CISO at a...
-
6
Django-filter and custom querysets Django-filter is a powerful tool, but the documentation is a little sparse. If you want to see examples of custo...
-
7
Using django-tables2, django-filter and django-crispy-forms together I was recently working on a very CRUDy prototype and decided to us...
-
5
Filter format for URL list-based policies 05/01/2020 3 minutes to read In this article This article describes the filter format used for the Microsoft...
-
2
Working with DateTime and format in laravel Hello everyone, Welcome back! Here am with another interesting topic, it is about DateTime and changing the format of it in our applications. We come across...
-
15
Php Datetime Format unknown - Date only advertisements I am working on extracting the date from filenames. For...
-
12
Convert the Column type from String to Datetime format in Pandas DataframeConvert the Column type from String to Datetime format in Pandas Dataframe10 Views30/05/2022...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK