[Answer]-Default form field is empty after post in Django

1👍

Update:

I figured that default=datetime.date.now behaved like auto_now_add=True because you mentioned that your template only contains the text field.

It turns out your situation is not handled any differently and you must display the date field in your form.

If you want to ignore it, you can override the ModelForm.save method to set the date yourself, or add it in your model save() definition.

You should look into the auto_now_add=True parameter in model field definitions which automatically adds this behavior of setting a date field to datetime.datetime.now() upon the first save.

date = models.DateTimeField(auto_now_add=True)

0👍

You should write

default = datetime.datetime.now()

with parentheses to call function ang get datetime. There is auto_now field to do this in better way. https://docs.djangoproject.com/en/dev/ref/models/fields/#datefield

These is another way to provide initial data from view:

form = CommentForm( initial = { 'date': datetime.datetime.now(), 'text': u'', }

Read more on https://docs.djangoproject.com/en/dev/ref/forms/fields/#initial

👤Sergei

Leave a comment