routing.BuildError,url_for 中赋值了filter,建立路由时报错filter无赋值。

bluelog

#1
这是浏览器上的报错
``` #### File "C:\Users\ASUS\exam\blog\bluelog\bluelog\templates\base.html", line  *57* , in  `block "nav"`<a class="dropdown-item" href="{{ url_for('admin.manage_comment') }}">
 #### File "c:\users\asus\exam\blog\bluelog\.venv\lib\site-packages\flask\helpers.py", line  *356* , in  `url_for`
return appctx.app.handle_url_build_error(error, endpoint, values)
 #### File "c:\users\asus\exam\blog\bluelog\.venv\lib\site-packages\flask\app.py", line  *2061* , in  `handle_url_build_error`
reraise(exc_type, exc_value, tb)
 #### File "c:\users\asus\exam\blog\bluelog\.venv\lib\site-packages\flask\_compat.py", line  *35* , in  `reraise`
raise value
 #### File "c:\users\asus\exam\blog\bluelog\.venv\lib\site-packages\flask\helpers.py", line  *345* , in  `url_for`
force_external=external)
 #### File "c:\users\asus\exam\blog\bluelog\.venv\lib\site-packages\werkzeug\routing.py", line  *1776* , in  `build`
raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'admin.manage_comment'. Did you forget to specify values ['filter']? 
视图代码
@admin_bp.route('/comment/manage/<filter>, methods=['GET'])
@login_required
def manage_comment(filter):
filter_rule=filter
page=request.args.get('page',1,type=int)
 per_page=current_app.config['BLUELOG_COMMENT_PER_PAGE']
if filter_rule=='unread':
    filtered_comments=Comment.query.filter_by(reviewed=False)
elif filter_rule;=='admin':
   filtered_comments&=Comment.query.filter_by(from_admin=True)
else:
filtered_comments=Comment.query
 pagination = filtered_comments.order_by(Comment.timestamp.desc()).paginate(page,per_page=per_page)
comments = pagination.items
return render_template('admin/manage_comment.html',comments=comments,pagination=pagination)
    html页面的代码
    ```    
    <li class="nav-item">
        <a class="nav-link {% if request.args.get('filter','all') == 'all' % }active{% endif %}"
        href="{{ url_for('admin.manage_comment',filter='all') }}"All </a></li>
    ```
    是原代码仓库的代码,没改过。 复制黏贴代码进来会有一些变成符号- - ,头痛。

#2

请重新编辑帖子,正确格式化代码块。


#3

帖子已更新


#4

基模板第 57 行的管理评论链接没有赋值 filter。

下次出错可以自己从报错信息里找到出错的文件,从下面这一行就能找到问题的关键:

File "C:\Users\ASUS\exam\blog\bluelog\bluelog\templates\base.html", line  *57* , in  `block "nav"`&lt;a class="dropdown-item" href="{{ url_for('admin.manage_comment') }}"

你的帖子格式真是乱 :expressionless:


#5

。。。点的预格式化文本 复制黏贴进来一些特殊符号会变成别的,头晕 :)


#6

明白了,谢谢。


#7

不客气。下次可以试着用三个连续反引号(英文输入状态):
```
保持前后的两行三个反引号占据单独一行,然后在中间粘贴你的代码块
```


#8

好的。