[FIXED] Why is my attempt to submit my form returning "Not a valid datetime value"


I created a form that allowed a user to select a desired date and time but when the form is submitted I got this error message

Not a valid datetime value

I tried including the desired format or the standard datetime format but I got the same result

class AddPostForm(FlaskForm):
    post_title = StringField('Share Title', validators=[DataRequired()])
    description = StringField('Description', validators=[DataRequired()])
    share_date = DateTimeField('Share Date', validators=[DataRequired()])


@posts_blueprint.route('/add', methods=['GET', 'POST'])
def add_post():
    form = AddPostForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            new_post = Post(form.post_title.data, form.description.data, 
                            current_user.id, form.share_date)

            flash('New shared Item, {}added!'.format(new_post.post_title), 
            return redirect(url_for('posts.user_posts'))
           flash('ERROR! Recipe was not added.', 'error')

   return render_template('add_post.html', title='Add Item', form=form)

{% from "_form_macros.html" import render_errors %}
{% import "bootstrap/utils.html" as utils %}
{% import "bootstrap/wtf.html" as wtf %}
{% extends "layout.html" %}
{% block body %}

<div class="content-section">
  <div class="page-header">
    <h2>Add a New Post</h2>

<div class="container">
  <form action="{{ url_for('posts.add_post') }}" method="post" 
      {{ form.csrf_token }}
<div class="form-group row">
  <legend for="post_title" class="col-form-legend col-sm-3">Title</legend>
  <div class="col-sm-6">
    <input type="text" class="form-control" id="post_title" 
    name="post_title" placeholder="Enter Post title...">
    {{ render_errors(form.post_title) }}

<div class="form-group row">
  <legend for="description" class="col-form-legend col-sm- 
  <div class="col-sm-6">
    <input type="text" class="form-control" id="description" 
    name="description" placeholder="Enter post description...">
    {{ render_errors(form.description) }}

<div class="form-group row">
<legend for="share_date" class="col-form-legend col-sm-3">Sharing 
<div class="col-sm-6">
  <input type="datetime-local" class="form-control" id="share_date" 
  name="share_date" placeholder="Sharing Date...">
  {{ render_errors(form.share_date) }}

<div class="center">
  <div class="offset-sm-2 col-sm-10">
    <button type="submit" class="btn btn-success">Add post</button>
{% endblock %}

The user should be able to input the desired date and time and submit the form successfully


It seems that there is a conflict between what your database expects and what your form is sending.
Try looking at what date format you are using in your model and also try printing out the form data to the console to see what it gives you.
Also try looking at the documentation for the wtforms to see what date type it uses and match that to the date field in your database model.

Answered By – Mahmoud Tokura

Answer Checked By – Willingham (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published