【发布时间】:2020-03-06 11:41:42
【问题描述】:
所以我正在关注 LinkedIn 的 Flask 教程:“使用 Flask 进行全栈 Web 开发 “。我在使用模板进行路由的主题上遇到了这个问题。我不知道是什么导致了这个问题,因为我对 Flask 很陌生,而且我在文档中没有看到任何关于它的内容。我的想法是我必须编写一些 Flask 无法运行的东西,语法方面,因为它是 Flask 自己的文件从编译器产生问题。我确信问题来自路由,因为错误发生在我之后在命令行上运行“flask run”,然后重新加载页面。
错误信息:
文件 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\app.py", 第 2446 行,在 wsgi_app 中 响应 = self.full_dispatch_request() 文件“c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\app.py”, 第 1951 行,在 full_dispatch_request 中 rv = self.handle_user_exception(e) 文件“c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\app.py”, 第 1820 行,在 handle_user_exception 中 reraise(exc_type, exc_value, tb) 文件“c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\_compat.py”, 第 39 行,在再加注中 提高价值文件“c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\app.py”, 第 1949 行,在 full_dispatch_request 中 rv = self.dispatch_request() 文件 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\app.py", 第 1935 行,在 dispatch_request 中 return self.view_functions[rule.endpoint](**req.view_args) 文件“C:\Users\user\Desktop\flask\enrollment\application\routes.py”,行 7、在索引 return render_template('index.html') File "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\templating.py", 第 137 行,在 render_template 中 return _render( 文件 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\template.py", 第 120 行,在 _render 中 rv = template.render(context) 文件 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\jinja2\asyncsupport.py", 第 76 行,在渲染中 返回 original_render(self, *args, **kwargs) 文件“c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\jinja2\environment.py”, 第 1008 行,在渲染中 return self.environment.handle_exception(exc_info, True) File "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\jinja2\environment.py", 第 780 行,在句柄异常中 reraise(exc_type, exc_value, tb) 文件“c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\jinja2\_compat.py”, 第 37 行,在再加注中 raise value.with_traceback(tb) 文件 "C:\Users\user\Desktop\flask\enrollment\application\templates\index.html", 第 16 行,在 {% include "includes/nav.html" %} 文件 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\jinja2\environment.py", 第 1005 行,在渲染中 返回 concat(self.root_render_func(self.new_context(vars))) 文件 "C:\Users\user\Desktop\flask\enrollment\application\templates\index.html", 第 14 行,根目录 文件 "C:\Users\user\Desktop\flask\enrollment\application\templates\includes\nav.html", 第 13 行,在根文件中 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\jinja2\runtime.py", 第 262 行,通话中 返回 __obj(*args, **kwargs) 文件 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\helpers.py", 第 370 行,在 url_for 返回 appctx.app.handle_url_build_error(error, endpoint, values) 文件 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\app.py", 第 2215 行,在 handle_url_build_error reraise(exc_type, exc_value, tb) 文件“c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\_compat.py”, 第 39 行,在再加注中 提高价值文件“c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\flask\helpers.py”, 第 357 行,在 url_for rv = url_adapter.build( 文件 "c:\users\user\desktop\flask\enrollment\venv\lib\site-packages\werkzeug\routing.py", 2020 年生产线,在建 raise BuildError(endpoint, values, method, self) werkzeug.routing.BuildError: 无法为端点构建 url '培训班'。你的意思是“索引”吗? 127.0.0.1 - - [2019 年 11 月 10 日 09:42:23] “GET /index HTTP/1.1”500 -routes.py
from application import app
from flask import render_template
@app.route('/index')
def index():
return render_template('index.html')
index.html:
<!DOCTYPE html>
<html>
<head>
<title>UTA - Home Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="static/css/main.css"/>
</head>
<body>
<div class="container-fluid text-center top-container">
<img src="static/images/uta-logo-200.png">
</div>
<div class="container">
{% include "includes/nav.html" %}
<div class="row">
<div class="col-md-12 text-center">
<h1>Welcome to Universal Tech Academy.</h1>
{% if login %}
<h3>Let's get started.</h3>
{% else %}
<p>Already registered? <a href="{{url_for('login') }}">Login</a></p>
{% endif %}
</div>
</div>
</div>
{% include "includes/footer.html" %}
</body>
</html>
【问题讨论】: