0
我正在開發一個WEB應用程序,並且在某些時候我想創建一個新的條目到我的Politic表。我可以做到這一點,直到我不得不添加一些日期。由於日期格式錯誤,表格不會接受輸入。讓我們看看吧..DateForm Python-Flask - [你沒有有效的日期值']}
我有這樣的形式:
class PoliticForm(FlaskForm):
publicName = StringField('Public Name', validators=[DataRequired("Please enter politician public name.")])
completeName = StringField('Complete Name', validators=[DataRequired("Please enter politician complete name.")])
startDate = DateField('Start Date', format='%m-%d-%Y', validators=[DataRequired("Please enter the politician start Date.")])
endDate = DateField('End Date', format='%m-%d-%Y', validators=(validators.Optional(),))
submit = SubmitField('Add Politician', validators=(validators.Optional(),))
但是溫柔的心不是的DateField正確的,因爲我的表格無法驗證的startDate場,也不是結束日期場。 這是我創造的功能:
@app.route("/create_politician", methods=["GET", "POST"])
@login_required
def create_politician():
form = PoliticForm()
if request.method == "POST":
print form.validate()
flash(form.errors)
if form.validate() == False:
return render_template('createPolitician.html', form=form)
else:
print form.startDate.data
newpolitician = Politic(form.publicName.data, form.completeName.data, form.startDate.data, form.endDate.data)
print form.startDate.data
db.session.add(newpolitician)
db.session.commit()
return redirect(url_for('home'))
elif request.method == "GET":
return render_template("createPolitician.html", form=form)
,並在該閃光燈(form.errors)
我得到如下:
{ '的startDate':'請輸入政治家開始日期 '], '結束日期。':[u'Not有效的日期值']}
models.py
class Politic(db.Model):
__searchable__ = ['publicName', 'completeName']
__tablename__ = 'politics'
idPolitician = db.Column(db.Integer, primary_key=True)
publicName = db.Column(db.String(150))
completeName = db.Column(db.String(300))
startDate = db.Column(db.Date)
endDate = db.Column(db.Date)
def __init__(self, publicName, completeName, startDate, endDate):
self.publicName = publicName.title()
self.completeName = completeName.title()
的.html
<form method="POST" action="/create_politician">
{{ form.hidden_tag() }}
<div class="form-group">
{{ form.publicName.label }}
{{ form.publicName }}
</div>
<div class="form-group">
{{ form.completeName.label }}
{{ form.completeName }}
</div>
<div class="form-group">
{{ form.startDate.label }}
{{ form.startDate }}
</div>
<div class="form-group">
{{ form.endDate.label }}
{{ form.endDate }}
</div>
{{ form.submit(class="btn-primary") }}
</form>
任何想法,什麼是錯的。有什麼方法來格式化日期輸入以匹配表單所要求的內容?
謝謝
問候