第 8 章 Bluelog 编辑文章报错:TypeError: 'method' object is not iterable

我在管理文章页面点击Edit,出现这样错误提示,哪里出错了啊?找不出原因

@admin_bp.route('/post/<int:post_id>/edit', methods=['GET', 'POST'])
@login_required
def edit_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.category = Category.query.get(form.category.data)
        db.session.commit()
        flash('Post updated.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    form.title.data = post.title
    form.body.data = post.body
    form.category.data = post.category_id
    return render_template('admin/edit_post.html', form=form)

请提供完整的错误回溯信息。另外可参考论坛说明了解如何插入代码块。

127.0.0.1 - - [13/Jan/2019 18:47:39] “GET /admin/post/10/edit HTTP/1.1” 500 -
Traceback (most recent call last):
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py”, line 2309, in call
return self.wsgi_app(environ, start_response)
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py”, line 2295, in wsgi_app
response = self.handle_exception(e)
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py”, line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_compat.py”, line 35, in reraise
raise value
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py”, line 2292, in wsgi_app
response = self.full_dispatch_request()
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py”, line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py”, line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_compat.py”, line 35, in reraise
raise value
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask\app.py”, line 1813, in full_dispatch_request
rv = self.dispatch_request()
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_debugtoolbar_init_.py”, line 125, in dispatch_request
return view_func(**req.view_args)
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\flask_login\utils.py”, line 261, in decorated_view
return func(*args, **kwargs)
File “D:\FlaskProjects\FlaskHTTP\bluelog\bluelog\blueprints\admin.py”, line 74, in edit_post
form = PostForm()
File “C:\Users\张启宏.virtualenvs\FlaskProjects-BArbVR56\lib\site-packages\wtforms\form.py”, line 212, in call
return type.call(cls, *args, **kwargs)
File “D:\FlaskProjects\FlaskHTTP\bluelog\bluelog\forms.py”, line 44, in init
for category in Category.query.order_by(Category.name).all]
TypeError: ‘method’ object is not iterable
127.0.0.1 - - [13/Jan/2019 18:47:39] “GET /admin/post/10/edit?debugger=yes&cmd=resource&f=style.css HTTP/1.1” 200 -
127.0.0.1 - - [13/Jan/2019 18:47:39] “GET /admin/post/10/edit?debugger=yes&cmd=resource&f=jquery.js HTTP/1.1” 200 -
127.0.0.1 - - [13/Jan/2019 18:47:39] “GET /admin/post/10/edit?debugger=yes&cmd=resource&f=debugger.js HTTP/1.1” 200 -
127.0.0.1 - - [13/Jan/2019 18:47:39] “GET /admin/post/10/edit?debugger=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1” 200 -
127.0.0.1 - - [13/Jan/2019 18:47:39] “GET /admin/post/10/edit?debugger=yes&cmd=resource&f=console.png HTTP/1.1” 200 -

class PostForm(FlaskForm):
    title = StringField('Title', validators=[DataRequired(), Length(1, 60)])
    category = SelectField('Category', coerce=int, default=1)
    body = CKEditorField('Body', validators=[DataRequired()])
    submit = SubmitField()

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.category.choices = [(category.id, category.name)
                                 for category in Category.query.order_by(Category.name).all]

你的 all() 方法调用漏掉了最后的括号。下次遇到类似问题,请先根据报错提示的位置和书中的代码或原项目源码进行对比。

好的,谢谢!是这个问题!