سؤال

I have a datetime field. While loading the initial value, I want to format the value from 2014-04-12 13:00:00 to April 12, 2014 1:00 pm. How can I do this?

هل كانت مفيدة؟

المحلول

You need to be clearer about what you want to do. You mean you want to change the way the date is displayed? You don't do that in the form at all, especially not in the __init__. Displaying values is the job of the widget.

The DateTimeWidget already accepts a format argument that controls the way the value is displayed. You can set this in the field declaration itself.

class MyModelForm(forms.ModelForm):
    datetime = forms.DateTimeField(widget=forms.DateTimeInput(format="%B %d, %Y %I:%M%p"))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top