I have a custom widget, and I want to format the value as Date.
The widget value from database(is a DateField) is the following format:
2018-04-10
and I want to be shown to user like this: Tue Apr 03 2018
I tried using a template filter:
{% if widget.value != None %} value="{{ widget.value|date:'M d, Y'}}"{% endif %}
but the field value gets empty.
When you pass date to the template, you do not need to convert it to any format
is the following format: 2018-04-10
Django date template filter takes an argument python datetime object and converts to the specified format (|date:'M d, Y')
I guess, widget.value
contains string '2018-04-10'
, in this case, django date template filter will not work.
You can write your own template filter to convert string to python datetime object and then apply date - (widget.value|own_filter|date:'M d, Y')
or, the best solution, check the widget argument, it should be python datetime object.
P.S. You can little improve your code:
{% if widget.value %} value="{{ widget.value|date:'M d, Y'}}"{% endif %}