WTForms - DateTimeLocalField data is None after submit
Asked Answered
N

3

8

After form is submitted with a POST request, every Field data has its value, except DateTimeLocalField. Accessing DateTimeLocalField's data value is a type of None.

Form

class ArticleForm(FlaskForm):
    name = StringField('Name', validators=[DataRequired()])
    category = SelectField(u'Category', choices=categories.choices)
    town = StringField('Town', validators=[DataRequired()])
    minimal_price = IntegerField('Minimal price')
    article_image = FileField('Article_image', validators=[FileRequired()])
    time_left = DateTimeLocalField('Time to end', validators=[InputRequired()],
                              format='%Y-%m-%d %H:%M:%S')
    description = TextAreaField('Description', validators=[DataRequired()])

Validation: (tested with is_submitted, all work except for article_form.time_left.data which is None)

if article_form.validate_on_submit():

    new_article = Article(name=article_form.name.data,
                          category=article_form.category.data,
                          town=article_form.town.data,
                          minimal_price=article_form.minimal_price.data,
                          article_image=name,
                          time_left=article_form.time_left.data, # <-- None
                          description=article_form.description.data,
                          user_id=current_user.id)

Any help to get data from DateTimeLocalField ?

Nielsen answered 15/10, 2018 at 22:47 Comment(0)
I
17

Try changing the format of the DateTimeLocalField from

format='%Y-%m-%d %H:%M:%S' 

to:

format='%Y-%m-%dT%H:%M'

Tip: you can print the actual content of the input field prior to the validation to confirm the correct formatting of the DateTimeLocalField field.

Impellent answered 16/10, 2018 at 18:3 Comment(1)
As to why this is needed: developer.mozilla.org/en-US/docs/Web/HTML/Element/input/… "One thing to note is that the displayed date and time formats differ from the actual value; the displayed date and time are formatted according to the user's locale as reported by their operating system, whereas the date/time value is always formatted YYYY-MM-DDThh:mm."Bokbokhara
K
6

Use wtforms.fields.html5.DateTimeLocalField instead of wtforms.DateTimeLocalField. Set the format with date and time separated by a 'T'. If you would want the current time as the default value, set default parameter.

from wtforms.fields.html5 import DateTimeLocalField

class InterviewForm(Form):
    posted = DateTimeLocalField('Posted:', default=datetime.today, format='%Y-%m-%dT%H:%M')
Kelley answered 16/3, 2019 at 18:16 Comment(0)
D
1

I did extensive research on the same problem, this is a hack but I still got the timestamp from the tag which looked like:

<input id="time_left" name="time_left" required type="datetime-local" value="2018-11-15T04:44">

You basically search for the timestamp from the tag returned by the tag

date = re.search('(\d{4})[/.-](\d{2})[/.-](\d{2})[T](\d{2})[:](\d{2})',
          str(form.time_left)).group())

Let me know if the solution worked for you or if found a better solution to the problem.

Disharmony answered 16/11, 2018 at 1:20 Comment(1)
Hi. I see you are new to stack so welcome. I got it working with answer above, I marked it as a complete. The solution you provided may work, but it's fairly complex. Python and WTForms have formating for dates and times, only it's not documented in WTForms, but in Python. That's the reason I didn't know how to format time. See more at table in link docs.python.org/3/library/…Nielsen

© 2022 - 2024 — McMap. All rights reserved.