关于werkzeug.routing.BuildError

时间:2016-08-30 09:02:46

标签: flask werkzeug

我有一个werkzeug.routing.BuildError :(' main.user',{},无) 在这里我的观点是

 $('.selectpicker').selectpicker({
     style: 'btn-info',
     size: 4
 });

 $('.selectpicker').on('change', function () {
     var opValue = $(this).val();
     console.log(opValue);
     $('#idSelect').val(opValue);
     $('.selectpicker').selectpicker('refresh');
 });

这是我的电话

@main.route('/user/<username>')
def user(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        abort(404)
    posts = user.posts.order_by(Post.timestamp.desc()).all()
    return render_template('user.html',user=user,posts=posts)

错误日志

url_for('.user',username=post.author.username)

我将username = post.author.username传递给了函数用户,错误显示为

File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "D:\mypython\anaconda\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\mypython\anaconda\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "d:\mypython\flask\flasky\app\main\views.py", line 25, in index
return render_template('index.html',form=form,posts=posts)
File "D:\mypython\anaconda\lib\site-packages\flask\templating.py", line 128, in render_template
context, ctx.app)
File "D:\mypython\anaconda\lib\site-packages\flask\templating.py", line 110, in _render
rv = template.render(context)
File "D:\mypython\anaconda\lib\site-packages\jinja2\environment.py", line 989, in render
return self.environment.handle_exception(exc_info, True)
File "D:\mypython\anaconda\lib\site-packages\jinja2\environment.py", line 754, in handle_exception
reraise(exc_type, exc_value, tb)
File "D:\mypython\anaconda\lib\site-packages\jinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "d:\mypython\flask\flasky\app\templates\index.html", line 3, in top-level template code
{% import 'bootstrap/wtf.html' as wtf %}
File "d:\mypython\flask\flasky\app\templates\base.html", line 1, in top-level template code
{% extends 'bootstrap/base.html' %}
File "D:\mypython\anaconda\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 1, in top-level template code
{% block doc -%}
File "D:\mypython\anaconda\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 4, in block "doc"
{%- block html %}
File "D:\mypython\anaconda\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 20, in block "html"
{% block body -%}
File "D:\mypython\anaconda\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 23, in block "body"
{% block content -%}
File "d:\mypython\flask\flasky\app\templates\base.html", line 43, in block "content"
{% block page_content %}{% endblock %}
File "d:\mypython\flask\flasky\app\templates\index.html", line 15, in block "page_content"
{% include '_posts.html' %}
File "d:\mypython\flask\flasky\app\templates\_posts.html", line 5, in top-level template code
<a href="{{ url_for('.user',username=post.author.username) }}">
File "D:\mypython\anaconda\lib\site-packages\flask\helpers.py", line 312, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "D:\mypython\anaconda\lib\site-packages\flask\app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "D:\mypython\anaconda\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "D:\mypython\anaconda\lib\site-packages\flask\helpers.py", line 305, in url_for
force_external=external)
File "D:\mypython\anaconda\lib\site-packages\werkzeug\routing.py", line 1678, in build
Display the sourcecode for this frameOpen an interactive python shell in this frameraise BuildError(endpoint, values, method)
{}没有{},为什么? 为什么它没有找到功能&#39;用户&#39;? 在我使用

之前代码进展顺利
werkzeug.routing.BuildError: ('main.user', {}, None)

{% include '_posts.html' %}

User.generate_fake()
Post.generate_fake()

here function generate_fake() is
    @staticmethod
    def generate_fake(count=100):
        from random import seed,randint
        import forgery_py

        seed()
        user_count = User.query.count()
        for i in range(count):
            u =User.query.offset(randint(0,user_count-1)).first()
            p = Post(body=forgery_py.lorem_ipsum.sentences(randint(1,3)),timestamp=forgery_py.date.date(True),author=u)
            db.session.add(p)
            db.session.commit()

这里可能出了什么问题?

这是我在stackoverflow中的第一个问题,非常感谢:)

0 个答案:

没有答案