Issue
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
#Form.py
class AddPostForm(FlaskForm):
post_title = StringField('Share Title', validators=[DataRequired()])
description = StringField('Description', validators=[DataRequired()])
share_date = DateTimeField('Share Date', validators=[DataRequired()])
#views.py
@posts_blueprint.route('/add', methods=['GET', 'POST'])
@login_required
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)
db.session.add(new_post)
db.session.commit()
flash('New shared Item, {}added!'.format(new_post.post_title),
'success')
return redirect(url_for('posts.user_posts'))
else:
flash_errors(form)
flash('ERROR! Recipe was not added.', 'error')
return render_template('add_post.html', title='Add Item', form=form)
#add_post.html
{% 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>
<div class="container">
<form action="{{ url_for('posts.add_post') }}" method="post"
enctype="multipart/form-data">
{{ 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>
</div>
<div class="form-group row">
<legend for="description" class="col-form-legend col-sm-
3">Description</legend>
<div class="col-sm-6">
<input type="text" class="form-control" id="description"
name="description" placeholder="Enter post description...">
{{ render_errors(form.description) }}
</div>
</div>
<div class="form-group row">
<legend for="share_date" class="col-form-legend col-sm-3">Sharing
Date</legend>
<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>
</div>
<div class="center">
<div class="offset-sm-2 col-sm-10">
<button type="submit" class="btn btn-success">Add post</button>
</div>
</div>
</form>
</div>
</div>
{% endblock %}
The user should be able to input the desired date and time and submit the form successfully
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.