数据不及时更新就是 类加载问题,因为view文件中调用form的时候,不会触发类属性的,所以要加init文件
# 如果不加入__init__函数会导致,SelectedField表单生成只有里面的内容不会和数据库同步(即数据库添加, # 删除字段时表单中数据项和初始化时一致.下一次重启app是才会同步)
class MovieForm(FlaskForm): tag_id = SelectField( label="标签", validators=[ DataRequired("请选择星标签!") ], coerce=int, description="标签", render_kw={ "class": "from-control", } ) def __init__(self): FlaskForm.__init__(self) self.tag_id.choices = [(usr.id, usr.name) for usr in Tag.query.all()] @admin.route("/movie/add/", methods=["GET", "POST"]) @admin_login_req def movie_add(): form = MovieForm() if form.validate_on_submit(): data = form.data movie = Movie( title=data["title"], url=data["url"], info=data["info"], logo=data["logo"], star=int(data["star"]), playnum=0, commentnum=0, tag_id=int(data["tag_id"]), area=data["area"], release_time=data["release_time"], length=data["length"], ) db.session.add(movie) db.session.commit() flash("添加电影成功!", "ok") return redirect(url_for("admin.movie_add")) return render_template("admin/movie_add.html", form=form)