DateFormat in Django and App Engine?

I have a little problem with dates in Django and Google App Engine :

I have the following class because I need a date input in DD / MM / YY format:

class MyForm(ModelForm): 
      mydate = forms.DateTimeField(input_formats=['%d-%m-%y', '%d/%m/%y']) 
      class Meta: 
          model = MyObject 

      

This works for logging into the data store. However, when I use the generic view to edit the data, the form is returned in YYYY-MM-DD format. Any ideas on how to change this?

+2


source to share


3 answers


forms.DateInput takes a format keyword argument, and this can be used to control the format that is presented (I seem to remember one way or another):

class MyForm(ModelForm): 
      mydate = forms.DateField(widget=forms.DateInput(format="%d/%m/%y")) 
      class Meta: 
          model = MyObject

      



I ended up subclassing both the field and the widget as I wanted to be able to control the formats even more.

+3


source


A DateTimeField

will return a value datetime.datetime

as its value, so you can use any of the normal methods defined in this module to format the data. In Django templates, you can use filters, date

or time

:

{{my_obj.mydate|date:"D d M Y"}}

      

Which prints something like:



Wed 09 Jan 2008

      

(See http://docs.djangoproject.com/en/dev/ref/templates/builtins/#date for details )

+1


source


I used

def date_format(self, instance, **kwargs):
 return getattr(instance,self.name) and getattr(instance,self.name).strftime('%d %m %Y')

from google.appengine.ext.db.djangoforms import DateProperty 
 DateProperty.get_value_for_form = date_format

      

0


source







All Articles