在 HTML 形式中使用 `url_for()` 时,Flask 无法识别控制器
Flask does not recognize controller when using `url_for()` in HTML form
在尝试实现基本表单时,我收到以下错误消息:werkzeug.routing.BuildError: Could not build url for endpoint 'finder.search'. Did you mean 'finder.static' instead?
,index.html
页面甚至因为这个错误而无法加载。
目录
Steam
SteamFinder
static
style.css
templates
index.html
result.html
__init__.py
finder.py
venv
init.py
from flask import Flask
from flask.templating import render_template
def create_app():
# create and configure the app
app = Flask(__name__)
from . import finder
app.register_blueprint(finder.bp)
@app.route("/")
def home():
return render_template("index.html")
return app
finder.py
from flask import Blueprint, request
from flask.templating import render_template
bp = Blueprint("finder", __name__, template_folder="templates", static_folder="static")
bp.route("/search", methods=["POST"])
def search():
query = request.form["query"]
print(query)
return render_template("result.html")
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../static/style.css"/>
<title>Home</title>
</head>
<body>
<form action="{{ url_for('finder.search') }}" method="post">
<label for="query">Query:</label><br>
<input type="text" id="query" name="query"><br>
<input type="submit">
</form>
</body>
</html>
您不能直接在模板中使用url_for
,Jinja 无法处理它。您应该将其作为变量传递给 render_template
函数,并在 jinja 块中使用该变量名称。
示例:
return render_template("index.html", form_action_url=url_for('finder.search'))
表单的操作将是:
<form action="{{ form_action_url }}" method="post">
<label for="query">Query:</label><br>
<input type="text" id="query" name="query"><br>
<input type="submit">
</form>
在尝试实现基本表单时,我收到以下错误消息:werkzeug.routing.BuildError: Could not build url for endpoint 'finder.search'. Did you mean 'finder.static' instead?
,index.html
页面甚至因为这个错误而无法加载。
目录
Steam
SteamFinder
static
style.css
templates
index.html
result.html
__init__.py
finder.py
venv
init.py
from flask import Flask
from flask.templating import render_template
def create_app():
# create and configure the app
app = Flask(__name__)
from . import finder
app.register_blueprint(finder.bp)
@app.route("/")
def home():
return render_template("index.html")
return app
finder.py
from flask import Blueprint, request
from flask.templating import render_template
bp = Blueprint("finder", __name__, template_folder="templates", static_folder="static")
bp.route("/search", methods=["POST"])
def search():
query = request.form["query"]
print(query)
return render_template("result.html")
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../static/style.css"/>
<title>Home</title>
</head>
<body>
<form action="{{ url_for('finder.search') }}" method="post">
<label for="query">Query:</label><br>
<input type="text" id="query" name="query"><br>
<input type="submit">
</form>
</body>
</html>
您不能直接在模板中使用url_for
,Jinja 无法处理它。您应该将其作为变量传递给 render_template
函数,并在 jinja 块中使用该变量名称。
示例:
return render_template("index.html", form_action_url=url_for('finder.search'))
表单的操作将是:
<form action="{{ form_action_url }}" method="post">
<label for="query">Query:</label><br>
<input type="text" id="query" name="query"><br>
<input type="submit">
</form>