Flask WTForm DateField仅接受今天和将来的日期



我有一个WT表单,它需要从前端输入数据。目前,该表单允许用户输入所有日期,但由于我正在创建一个只允许输入未来日期的网站,我想在此添加一个验证器,不允许输入过去的日期。

我已经看了以前的问题,但是,它们并没有太大的帮助。

下面是我的表格.py 的一部分

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, DateField
from wtforms.validators import DataRequired
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from ..models import TasterDay, Course

class TasterDayForm(FlaskForm):
"""
Form for admin to add or edit a taster day
"""
name = StringField('Name', validators=[DataRequired()])
date = DateField('Date (DD-MM-YYYY)', format='%d-%m-%Y')
course = StringField('Course', validators=[DataRequired()])
lecturer = StringField('Lecturer', validators=[DataRequired()])
description = StringField('Description', validators=[DataRequired()])
submit = SubmitField('Submit')

这是我的views.py文件中的一部分,其中显示了一些路线。

from flask import abort, flash, redirect, render_template, url_for
from flask_login import current_user, login_required
from . import admin
from .forms import TasterDayForm, StudentAssignForm, CourseForm
from .. import db
from ..models import TasterDay, Course, Student

def check_admin():
"""
Prevent non-admins from accessing the page
"""
if not current_user.is_admin:
abort(403)

# TasterDay Views
@admin.route('/tasterdays', methods=['GET', 'POST'])
@login_required
def list_tasterdays():
"""
List all tasterdays
"""
check_admin()
tasterdays = TasterDay.query.all()
return render_template('admin/tasterdays/tasterdays.html',
tasterdays=tasterdays, title="TasterDay")

@admin.route('/tasterdays/add', methods=['GET', 'POST'])
@login_required
def add_tasterdays():
"""
Add a Tasterday to the database
"""
check_admin()
add_tasterdays = True
form = TasterDayForm()
if form.validate_on_submit():
tasterdays = TasterDay(name=form.name.data,
description=form.description.data,
date=form.date.data,
course=form.course.data,
lecturer=form.lecturer.data)
try:
# add tasterday to the database
db.session.add(tasterdays)
db.session.commit()
flash('You have successfully added a new Taster Day.')
except:
# in case tasterday  already exists
flash('Error: Taster Day already exists.')
# redirect to tasterdays page
return redirect(url_for('admin.list_tasterdays'))
# load tasterdays template
return render_template('admin/tasterdays/tasterday.html', action="Add",
add_tasterdays=add_tasterdays, form=form,
title="Add Taster Day")

@admin.route('/tasterdays/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_tasterdays(id):
"""
Edit a TasterDay
"""
check_admin()
add_tasterdays = False
tasterdays = TasterDay.query.get_or_404(id)
form = TasterDayForm(obj=tasterdays)
if form.validate_on_submit():
tasterdays.name = form.name.data
tasterdays.description = form.description.data
tasterdays.date = form.date.data
tasterdays.course = form.course.data
tasterdays.lecturer = form.lecturer.data
db.session.commit()
flash('You have successfully edited the TasterDay.')
# redirect to the tasterdays page
return redirect(url_for('admin.list_tasterdays'))
form.description.data = tasterdays.description
form.name.data = tasterdays.name
form.date.data = tasterdays.date
form.course.data = tasterdays.course
form.lecturer.data = tasterdays.lecturer
return render_template('admin/tasterdays/tasterday.html', action="Edit",
add_tasterdays=add_tasterdays, form=form,
tasterdays=tasterdays, title="Edit TasterDay")

有人能解释一下我需要添加什么来确保过去的日期不能输入到表格中吗?如果是,就会出现错误。

谢谢

我自己设法找到了答案。

我在forms.py页面的顶部添加了import datetime

就在我的class TasterDayForm(FlaskForm):下方

我补充道:

def validate_date(form, field):
if field.data < datetime.date.today():
raise ValidationError("The date cannot be in the past!")

这是有效的,如果用户输入的日期已过,则会引发错误。

相关内容

  • 没有找到相关文章

最新更新