Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input_formats to DateTimeField

I have a DateTimeField :

 start_time=forms.DateTimeField(input_formats='%y-%m-%d %H:%M')

and in html

   {{form.start_time}}

but no matter what i entre in th field, like: 2013-07-07 19:00 it will always give me an error : enter valid date/time, what is worng here? Thanks in Advance

like image 360
hln Avatar asked Apr 24 '13 20:04

hln


People also ask

How do I enter DateTimeField in Django?

DateTimeField in Django Forms is a date field, for taking input of date and time from user. The default widget for this input is DateTimeInput. It Normalizes to: A Python datetime. datetime object.

What is DateTimeField?

DateTimeField is a date and time field which stores date, represented in Python by a datetime. datetime instance. As the name suggests, this field is used to store an object of datetime created in python. The default form widget for this field is a TextInput .

What is the format of Django DateTimeField?

DATETIME_FORMAT="%Y-%m-%d%H:%M:%S"

How do you remove this field is required Django?

If yes try to disable this behavior, set the novalidate attribute on the form tag As <form action="{% url 'new_page' %}", method="POST" novalidate> in your html file.


2 Answers

Write the definition like this:

start_time=forms.DateTimeField(input_formats=['%Y-%m-%d %H:%M'])

Note that input_formats is a list and the Y is upper case. That should work.

like image 176
Paulo Bu Avatar answered Sep 29 '22 16:09

Paulo Bu


Just did some trial and error. When specifying input_formats you have to consider that for a widget format='whatevers in here' is a string whereas input_formats=['whatevers in here', 'whatever else'] is a list.

like image 20
thosehippos Avatar answered Sep 29 '22 17:09

thosehippos