几个小时后,我无法解决此问题。我想要实现的是为每个用户配置文件创建一个自定义URL(例如:www.site.com/ user / <用户名> ),但是调用render_template("user.html")
会得到一个{{1} },表示我可能忘记了指定werkzeug.routing.BuildError
。但是这个['username']
是否不应该来自URL本身?这是在我尝试访问['username']
时发生的(我还尝试了其他随机用户名),但无法做到,尤其是在使用/user/test
时。
完整的追溯:
render_template
来自Traceback (most recent call last):
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask_login\utils.py", line 261, in decorated_view
return func(*args, **kwargs)
File "D:\Users\Luke A\Documents\Dev\flask\HomeServer\homeServer\routes.py", line 51, in user
return render_template("user.html", username=username)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\templating.py", line 135, in render_template
context, ctx.app)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\templating.py", line 117, in _render
rv = template.render(context)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\jinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "D:\Users\Luke A\Documents\Dev\flask\HomeServer\homeServer\templates\user.html", line 1, in top-level template code
{% extends "base.html" %}
File "D:\Users\Luke A\Documents\Dev\flask\HomeServer\homeServer\templates\base.html", line 27, in top-level template code
<li class="nav-item {% if url_for(request.endpoint) == '/index' %}active{% endif %}">
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\helpers.py", line 356, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\app.py", line 2061, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "d:\users\luke a\documents\dev\flask\homeserver\env\lib\site-packages\flask\helpers.py", line 345, in url_for
force_external=external)
File "d:\users\luke a\documents\dev\flask\homeserver\env\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 'user'. Did you forget to specify values ['username']?
的代码段:
routes.py
来自@homeServer.route("/user/<username>")
@login_required
def user(username):
return render_template("user.html")
的摘要:
templates/user.html
使用{% extends "base.html" %}
{% block content %}
<h1 class="font-weight-light">User page</h1>
{% endblock %}
代替return "Hello, World!"
很好。
return render_template("user.html")
仅包含bootstrap包含项和导航栏。