首页 > werkzeug.routing.BuildError: ('main.post', {}, None)

werkzeug.routing.BuildError: ('main.post', {}, None)

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python34\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python34\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python34\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python34\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python34\lib\site-packages\flask\app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python34\lib\site-packages\flask_login.py", line 792, in decorated_view
    return func(*args, **kwargs)
  File "D:\PyCharm_Workspace\WebBlogold\app\main\views.py", line 167, in edit
    return redirect(url_for('.post',id=post.id))
  File "C:\Python34\lib\site-packages\flask\helpers.py", line 312, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1641, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python34\lib\site-packages\flask\helpers.py", line 305, in url_for
    force_external=external)
  File "C:\Python34\lib\site-packages\werkzeug\routing.py", line 1678, in build
    raise BuildError(endpoint, values, method)
werkzeug.routing.BuildError: ('main.post', {}, None)

这是我的view

@main.route('/post/<int:id>',methods=['GET','POST'])
def post(id):
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(body=form.body.data,post=post,author=current_user._get_current_object())
        db.session.add(comment)
        flash('your comment has been published.')
        return redirect(url_for('.post',id=post.id,page=-1))
    page = request.args.get('page', 1, type=int)
    if page == -1:
        page = (post.comments.count() - 1) // 10 + 1
    pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(page, per_page=10,error_out=False)
    comments = pagination.items
    return render_template('post.html',posts=[post],form=form,comments=comments,pagination=pagination)
    
@main.route('/edit/<int:id>',methods=['GET','POST'])
@login_required
def edit(id):
    post = Post.query.get_or_404(id)
    if not current_user.can(Permission.ADMINISTER):
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,label=form.labels.data,body=form.body.data)
        if Label.query.filter_by(name=form.labels.data).first() is None:
            label = Label(name=form.labels.data)
            db.session.add(label)
        db.session.add(post)
        flash('The post has been updated.')
        return redirect(url_for('.post',id=post.id))
    form.body.data = post.body
    form.title.data = post.title
    form.labels.data = post.label
    return render_template('edit_post.html',form=form)

求解,困扰了很久,希望能顺便讲解一下url_for的用法,谢谢!!!


请问你是怎么注册Blueprint的?你的代码只贴出了view,却没有看到注册路由这一块!


简单的理解就是,就只需要url_for里面的东西是一个函数
stackoverflow


应该是没有提供参数。

url_for('main.post', id=10)
【热门文章】
【热门文章】