默认值哪些是需要在movie_edit.html中填写的?
来源:7-9 .电影管理-列表-删除-编辑2
慕仔9271372
2017-09-20
老师,在视频中form.star(value=movie.star),但是在最后的代码中却是form.star,不需要默认值,到底什么时候需要默认值呢?还比如form.tag_id(value=movie.id) form.info(value=movie.info)
写回答
1回答
-
rustgopy
2017-09-21
同学,你好,默认值可以通过前端的html赋值,也可以通过后端进行动态赋值。
# 编辑电影 @admin.route("/movie/edit/<int:id>/", methods=["GET", "POST"]) @admin_login_req # @admin_auth def movie_edit(id=None): form = MovieForm() form.url.validators = [] form.logo.validators = [] movie = Movie.query.get_or_404(int(id)) if request.method == "GET": form.info.data = movie.info form.tag_id.data = movie.tag_id form.star.data = movie.star if form.validate_on_submit(): data = form.data movie_count = Movie.query.filter_by(title=data["title"]).count() if movie_count == 1 and movie.title != data["title"]: flash("片名已经存在!", "err") return redirect(url_for('admin.movie_edit', id=id)) if not os.path.exists(app.config["UP_DIR"]): os.makedirs(app.config["UP_DIR"]) os.chmod(app.config["UP_DIR"], "rw") if form.url.data.filename != "": file_url = secure_filename(form.url.data.filename) movie.url = change_filename(file_url) form.url.data.save(app.config["UP_DIR"] + movie.url) if form.logo.data.filename != "": file_logo = secure_filename(form.logo.data.filename) movie.logo = change_filename(file_logo) form.logo.data.save(app.config["UP_DIR"] + movie.logo) movie.star = data["star"] movie.tag_id = data["tag_id"] movie.info = data["info"] movie.title = data["title"] movie.area = data["area"] movie.length = data["length"] movie.release_time = data["release_time"] db.session.add(movie) db.session.commit() flash("修改电影成功!", "ok") return redirect(url_for('admin.movie_edit', id=id)) return render_template("admin/movie_edit.html", form=form, movie=movie)
这一步就是通过后台动态赋值的
form.info.data = movie.info
form.tag_id.data = movie.tag_id
form.star.data = movie.star012017-09-21
相似问题