已解决,打扰了!!!
来源:7-6 .电影管理-添加电影2
江世民
2017-08-26
在搭建后台管理的添加电影时,一直有这个错误,是不是没有引入包?
以下是views.py
from . import admin
from flask import render_template,redirect,url_for,flash,session,request
from app.admin.forms import LoginForm,TagForm,MovieForm
from app.models import Admin,Tag,Movie
from functools import wraps
from app import db,app
from werkzeug.utils import secure_filename
import os,stat
import uuid #生成唯一字符串
import datetime
#添加电影
@admin.route("/movie/add",methods=["GET","POST"])
@admin_login_req
def movie_add():
form = MovieForm()
if form.validate_on_submit:
data = form.data
file_url = secure_filename(form.url.data.filename)
file_logo = secure_filename(form.logo.data.filename)
if not os.path.exists(app.config["UP_DIR"]):
os.makedirs(app.config["UP_DIR"]) #创建目录
os.chmod(app.config["UP_DIR"],stat.S_IROTH)
os.chmod(app.config["UP_DIR"],stat.S_IWOTH)
url = change_filename(file_url)
logo = change_filename(file_logo)
form.url.data.save(app.config["UP_DIR"]+url) #保存文件
form.logo.data.save(app.config["UP_DIR"] + logo) #保存封面
movie = Movie(
title = data["title"],
url = url,
info = data["info"],
logo = logo,
star = data["star"],
playnum = 0,
commentnum = 0,
tag_id = 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)
1回答
-
同学,你好,注意检查下form.validate_on_submit,应该写成form.validate_on_submit():
# 添加电影 @admin.route("/movie/add/", methods=["GET", "POST"]) @admin_login_req # @admin_auth def movie_add(): form = MovieForm() form.tag_id.choices = [(v.id, v.name) for v in Tag.query.all()] #标签及时更新 if form.validate_on_submit(): data = form.data file_url = secure_filename(form.url.data.filename) file_logo = secure_filename(form.logo.data.filename) if not os.path.exists(app.config["UP_DIR"]): os.makedirs(app.config["UP_DIR"]) os.chmod(app.config["UP_DIR"], "rw") url = change_filename(file_url) logo = change_filename(file_logo) form.url.data.save(app.config["UP_DIR"] + url) form.logo.data.save(app.config["UP_DIR"] + logo) movie = Movie( title=data["title"], url=url, info=data["info"], logo=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)
012017-08-26
相似问题