【发布时间】:2020-06-24 11:57:12
【问题描述】:
由于某种原因,url_for 命令在“about”路径中运行良好,但在“home”路径中却没有。
请看下面的代码sn-p: 文件:templates/layout.html(这是主模板站点)
<a class="nav-link" href="http://127.0.0.1:5000/home">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<!--<a class="nav-link" href="http://127.0.0.1:5000/about">About</a>-->
<a class="nav-link" href="{{url_for('about')}}">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Help</a>
</li>
</ul>
</div>
</nav>
这很好用,我可以在 About 和 Home 之间切换。
但是,当我将“主页”链接更改为使用 url_for 时,它就不起作用了。
<a class="nav-link" href="{{url_for('home')}}">Home <span class="sr-only">(current)</span></a>
给出这个错误:
jinja2.exceptions.TemplateSyntaxError
jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'string'
还有这个:
File "E:\Python installation\myproject\myflaskproject\templates\layout.html", line 26, in template
<a class="nav-link" href="{{url_for('home')">Home <span class="sr-only">(current)</span></a>
jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'string'
我添加了额外的花括号,但仍然出现错误:
<a class="nav-link" href="{{url_for('home')}}">Home <span class="sr-only">(current)</span></a>
错误:
File "E:\Python installation\myproject\env\Lib\site-packages\werkzeug\routing.py", line 2179, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'home'. Did you mean 'hello_world' instead?
另一个错误:
File "E:\Python installation\myproject\myflaskproject\templates\layout.html", line 26, in top-level template code
<a class="nav-link" href="{{url_for('home')}}">Home <span class="sr-only">(current)</span></a>
为了修复代码,我无法理解这两个错误。
最后,使用这段代码,我什至无法打开 localhost/about 路由,因为它给出了这个错误:
templates/layout.html(这是主模板站点)
<title>Laptop review site</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#"><b><font color="#ffc60a">LR</font></b></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="{{url_for('home')}}">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<!--<a class="nav-link" href="http://127.0.0.1:5000/about">About</a>-->
<a class="nav-link" href="{{url_for('about')}}">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Help</a>
</li>
</ul>
</div>
</nav>
werkzeug.routing.BuildError
werkzeug.routing.BuildError: Could not build url for endpoint 'home'. Did you mean 'hello_world' instead?
我想知道这是否与它有关。这是定义和调用路由的地方:
laptopreview.py
@app.route('/') #this is what we type into our browser to go to pages. we create these using routes
@app.route('/home')
def hello_world():
#we will now have access to this reviwes data in our template
return render_template('home.html',reviews=reviews)
最后,如果有用的话,这里是整个错误日志,转到 url:http://127.0.0.1:5000/home
整个错误日志
werkzeug.routing.BuildError
werkzeug.routing.BuildError: Could not build url for endpoint 'home'. Did you mean 'hello_world' instead?
Traceback (most recent call last)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "E:\Python installation\myproject\myflaskproject\laptopreview.py", line 32, in hello_world
return render_template('home.html',reviews=reviews)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\templating.py", line 137, in render_template
return _render(
File "E:\Python installation\myproject\env\Lib\site-packages\flask\templating.py", line 120, in _render
rv = template.render(context)
File "E:\Python installation\myproject\env\Lib\site-packages\jinja2\environment.py", line 1090, in render
self.environment.handle_exception()
File "E:\Python installation\myproject\env\Lib\site-packages\jinja2\environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "E:\Python installation\myproject\env\Lib\site-packages\jinja2\_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "E:\Python installation\myproject\myflaskproject\templates\home.html", line 1, in top-level template code
{% extends "layout.html" %}
File "E:\Python installation\myproject\myflaskproject\templates\layout.html", line 26, in top-level template code
<a class="nav-link" href="{{url_for('home')}}">Home<span class="sr-only">(current)</span></a>
File "E:\Python installation\myproject\env\Lib\site-packages\flask\helpers.py", line 370, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\app.py", line 2216, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "E:\Python installation\myproject\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\Python installation\myproject\env\Lib\site-packages\flask\helpers.py", line 357, in url_for
rv = url_adapter.build(
File "E:\Python installation\myproject\env\Lib\site-packages\werkzeug\routing.py", line 2179, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'home'. Did you mean 'hello_world' instead?
【问题讨论】:
标签: html css flask routes url-for