Using the Django framework (1.3.1), together with Mongoengine.
When trying to save a posted field (the due date), it bails out with a
ValidationError (cannot parse date "2013-12-31": ['DueDate'])
However when saving the date via datetime.datetime.now()
it works fine. After searching for examples, I'm out of options.
The related parts of my code (with a normal HTML form using the text input tag):
views.py
goal.DueDate = request.POST['duedate']
goal.save()
models.py
class Goal(Document):
DueDate = DateTimeField()
last_update = DateTimeField(required=True)
Any idea?
Update (can't answer myself yet):
Ok.. found the solution. Typing it, apparently gave new insights.
goal.DueDate = datetime.datetime.strptime(request.POST['duedate'], '%Y-%m-%d')
DateTimeField
expects a datetime, not a string.
If the format is well known, you can use strptime like in your update, or dateutil parse
method which is able to guess format.
You should also think about adopting a safer ISO formatted string send in the form from the web side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With