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
4条答案
按热度按时间wgx48brx1#
数据库所需的数据与表单所发送的数据之间似乎存在冲突。请尝试查看模型中使用的日期格式,并尝试将表单数据打印到控制台,以查看它提供的数据。此外,请尝试查看wtforms的文档,以查看它使用的日期类型,并将其与数据库模型中的日期字段相匹配。
ee7vknir2#
You may need to check the format of the entered date matches what the
DateTimeField
is expecting.See: DateTimeField Docs
Where the default format is:
'%Y-%m-%d %H:%M:%S'
If you know the format you're submitting, you can provide it when you create the field object:
ubby3x7f3#
If you are using
type="datetime-local"
in you template you need to first makeing sure you're importingDateTimeLocalField
notDateTimeField
and then in my case i was also imported incorrectly:Instead of:
Its now working fine and not returning None to BackEnd!
template.html
myapp.py
gev0vcfq4#
我也遇到了同样的问题。我通过将窗体中DateLocalField的格式更改为以下格式来修复它: