一、登陆装饰器
from functools import wraps # 登陆 def admin_login_req(f): @wraps(f) def decorated_function(*args, **kwargs): if "user" not in session: return redirect(url_for("admin.login", next=request.url)) # 获取到要跳转到的地址 return f(*args, **kwargs) return decorated_function
二、编辑页面
# 编辑电影 @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(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(): 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: 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: file_logo = secure_filename(form.logo.data.filename) movie.logo = change_filename(file_logo) # 文件保存 form.logo.data.save(app.config["UP_DIR"] + movie.logo) data = form.data movie.star = data["star"] 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)
三、html模板
<label for="input_title">{{ form.title.label }}</label> {{ form.title(value=movie.title) }} {% for err in form.title.errors %} <div class="col-md-12"> <span style="color: red">{{ err }}</span> </div> {% endfor %}