如题,我在参考 @greyli 的《Flask Web开发实践》一书的bluelog相关章节编写一个博客,编写完“创建分类”和“创建文章”页面的数据模型、表单、HTML页面,运行并访问时遇到“TypeError: ‘NoneType’ object is not iterable” 的错误,无论百度、Google、贴吧、QQ群以及微信群都求助过,没有得到答案。希望能在这里得到答案,谢谢。
错误截图:
视图代码:
#创建分类视图
@admin_bp.route('/category/create', methods=['GET','POST'])
def new_category():
form = Category_Form()
if form.validate_on_submit():
categoryname = form.category_name.data
categoryaka = form.category_aka.data
parentcategoryid = form.parent_category_id.data
new_cat = Category(category_name=categoryname, category_aka=categoryaka, parent_category_id=parentcategoryid)
db.session.add(new_cat)
db.session.commit()
return redirect(url_for('.category_list'))
return render_template('admin/new_category.html',form=form)
#创建文章视图
@admin_bp.route('/post/new_post',methods=['GET','POST'])
@login_required
def new_post():
form = Post_Form()
if form.validate_on_submit():
title = form.post_title.data
category = Category.query.get(form.post_category.data)
thumb = form.post_thumb.data
text = form.post_content.data
post = Post(post_title=title,category_id=category,post_thumb=thumb,post_content=text)
db.session.add(post)
db.session.commit()
return redirect(url_for('.post_list'))
return render_template('admin/create_post.html', form=form)
数据模型代码:
# 分类数据模型
class Category(db.Model):
__tablename__='category'
category_id = db.Column(db.Integer,primary_key=True,autoincrement=True,nullable=False,index=True,unique=True)
category_name = db.Column(db.String(256),nullable=False,unique=True)
category_aka = db.Column(db.String(256),nullable=False,unique=True)
parent_category_id = db.Column(db.Integer)
posts = db.relationship('Post',backref='category')
#文章数据模型
class Post(db.Model):
__tablename__='post'
post_id = db.Column(db.Integer,primary_key=True,autoincrement=True,nullable=False,index=True,unique=True)
post_title = db.Column(db.String(256),nullable=False)
post_thumb = db.Column(db.Text,nullable=False,default='https://api.uomg.com/api/rand.avatar?sort=女&format=jpg')
publish_datetime = db.Column(db.DateTime,default=datetime.now())
post_content = db.Column(db.Text,nullable=False)
post_url = db.Column(db.String(256),nullable=False)
post_view = db.Column(db.Integer,nullable=False,default=0)
tag_id = db.relationship('Tags', secondary=post_tags, backref=db.backref('posts'))
category_id = db.Column(db.Integer, db.ForeignKey('category.category_id'))
# category = db.relationship('Category', back_populates='postss')
user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'))
comments = db.relationship('Comment',backref='thepost',cascade='all,delete-orphan')
表单代码:
#分类表单
class Category_Form(FlaskForm):
category_name = StringField('分类名称',validators=[DataRequired(message='必填项')],render_kw={'placeholder':'分类名称'})
category_aka = StringField('分类别名',validators=[DataRequired(message='必填项,必须是英文和字母的结合')],render_kw={'placeholder':'分类别名'})
parent_category_id = SelectField('父级分类',validators=[DataRequired()],render_kw={'placeholder':'父级分类'})
#文章表单
class Post_Form(FlaskForm):
post_title = StringField('文章标题',validators=[DataRequired(message='文章标题必填!')],render_kw={'placeholder':''})
post_category = SelectField('文章分类', coerce=int,default=1,validators=[DataRequired(message='分类获取出错!')])
post_content = CKEditorField('文章正文',validators=[DataRequired(message='文章内容必写!')],render_kw={'placeholder':'正文内容'})
post_thumb = FileField('文章封面',validators=[DataRequired(message='必须上传缩略图!')])
post_tag = StringField('文章标签',validators=[DataRequired(message='文章必须有标签!')])
def __init__(self,*args,**kwargs):
super(Post_Form, self).__init__(*args,**kwargs)
self.post_category.choices = [(category.category_id,category.category_name)
for category in Category.query.order_by(Category.category_name).all()]
经测试,数据库连接正常,写的查询页面(文章列表及分类列表)均有数据输出(数据库中直接写入)。
在视图代码中,使用print()
语句作为判断,程序仅执行到form=Post_Form()
就开始报错,后面的if form.validate_on_submit():
并没有被运行。
希望 @greyli 在百忙之中帮忙指正错误,谢谢!
如果有其他小伙伴也能够指出,请您不吝赐教,万分感谢!