我们应该在指向 Flask 中的蓝图页面的表单操作 url_for 属性中传递什么参数?
what parameter should we pass in form action url_for attribute that points to a buleprint page in flask?
我将我的 python 应用程序转换为使用功能结构蓝图,我在 HTML 中有一个订阅表单,因此当我们调用 localhost/subscribe 路由时,我们读取表单输入并添加订阅用户
HTML代码:
<form method="POST" action="{{ url_for('subscribe') }}"> <!--{{ url_for('subscribe') }} -->
<div class="container">
<h2>Subscribe to our Newsletter</h2>
<p>Lorem ipsum..</p>
</div>
<div class="container" style="background-color:white">
<input type="text" placeholder="Name" name="name" required>
<input type="email" placeholder="Email address" name="mail" required>
<label>
<input type="checkbox" checked="checked" name="subscribe"> Daily Newsletter
<select>
<option></option>
</select>
</label>
</div>
<div class="container">
<input type="submit" value="submit">
</div>
<div>
<span>{{result}}</span>
</div>
</form>
subcribe.py 蓝图文件如下所示:
from flask import Blueprint, render_template, request
from DBConnection import Mongodb
subscribe_blueprint = Blueprint('subscribe', __name__)
@subscribe_blueprint.route('/subscribe', methods=['GET', 'POST'])
def subscribe():
print(request.method)
if request.method == "POST":
print('inside POST')
subscribers = get_subscribers_collection()
subscribe_date = datetime.utcnow
subscriber_email, subscriber_name = read_form_data()
existing_subscriber = is_subscribed(subscribers, subscriber_email)
if existing_subscriber is None:
add_subscriber(subscribers, subscriber_name, subscriber_email)
else:
result = {'email': subscriber_email + 'Alreday sucscribed!'}
return render_template("subscribe/subscribe.html", result=result)
else:
return render_template("subscribe/subscribe.html", result="{'key':'value'}")
并且在app.y我注册了它
from views.subscribe import subscribe_blueprint
app.register_blueprint(subscribe_blueprint)
当我调用路由 http://127.0.0.1:5000/subscribe 时:
出现以下错误:
Traceback (most recent call last):
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\fr-pa\Desktop\content_aggregator\views\subscribe.py", line 27, in subscribe
return render_template("subscribe/subscribe.html", result="{'alaa':'dddd'}")
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\templating.py", line 137, in render_template
return _render(
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\templating.py", line 120, in _render
rv = template.render(context)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\jinja2\environment.py", line 1090, in render
self.environment.handle_exception()
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\jinja2\environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\jinja2\_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "C:\Users\fr-pa\Desktop\content_aggregator\templates\subscribe\subscribe.html", line 1, in top-level template code
{% extends "layout.html" %}
File "C:\Users\fr-pa\Desktop\content_aggregator\templates\layout.html", line 35, in top-level template code
<div id="content">{% block content %}{% endblock %}</div>
File "C:\Users\fr-pa\Desktop\content_aggregator\templates\subscribe\subscribe.html", line 11, in block "content"
<form method="POST" action="{{ url_for('subscribe') }}"> <!--{{ url_for('subscribe') }} -->
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\helpers.py", line 370, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2216, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\helpers.py", line 357, in url_for
rv = url_adapter.build(
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\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 'subscribe'. Did you mean 'subscribe.subscribe' instead?
要在蓝图中构建URL,您需要指定蓝图的名称。传递给 url_for()
的端点应采用 <blueprint_name>.<view_function_name>
的形式。在您的情况下,此值为 subscribe.subscibe
.
action="{{ url_for('subscribe.subscribe') }}"
P.S。当前蓝图中使用url_for
时,可以将端点值缩短为.subscribe
.
我将我的 python 应用程序转换为使用功能结构蓝图,我在 HTML 中有一个订阅表单,因此当我们调用 localhost/subscribe 路由时,我们读取表单输入并添加订阅用户 HTML代码:
<form method="POST" action="{{ url_for('subscribe') }}"> <!--{{ url_for('subscribe') }} -->
<div class="container">
<h2>Subscribe to our Newsletter</h2>
<p>Lorem ipsum..</p>
</div>
<div class="container" style="background-color:white">
<input type="text" placeholder="Name" name="name" required>
<input type="email" placeholder="Email address" name="mail" required>
<label>
<input type="checkbox" checked="checked" name="subscribe"> Daily Newsletter
<select>
<option></option>
</select>
</label>
</div>
<div class="container">
<input type="submit" value="submit">
</div>
<div>
<span>{{result}}</span>
</div>
</form>
subcribe.py 蓝图文件如下所示:
from flask import Blueprint, render_template, request
from DBConnection import Mongodb
subscribe_blueprint = Blueprint('subscribe', __name__)
@subscribe_blueprint.route('/subscribe', methods=['GET', 'POST'])
def subscribe():
print(request.method)
if request.method == "POST":
print('inside POST')
subscribers = get_subscribers_collection()
subscribe_date = datetime.utcnow
subscriber_email, subscriber_name = read_form_data()
existing_subscriber = is_subscribed(subscribers, subscriber_email)
if existing_subscriber is None:
add_subscriber(subscribers, subscriber_name, subscriber_email)
else:
result = {'email': subscriber_email + 'Alreday sucscribed!'}
return render_template("subscribe/subscribe.html", result=result)
else:
return render_template("subscribe/subscribe.html", result="{'key':'value'}")
并且在app.y我注册了它
from views.subscribe import subscribe_blueprint
app.register_blueprint(subscribe_blueprint)
当我调用路由 http://127.0.0.1:5000/subscribe 时: 出现以下错误:
Traceback (most recent call last):
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\fr-pa\Desktop\content_aggregator\views\subscribe.py", line 27, in subscribe
return render_template("subscribe/subscribe.html", result="{'alaa':'dddd'}")
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\templating.py", line 137, in render_template
return _render(
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\templating.py", line 120, in _render
rv = template.render(context)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\jinja2\environment.py", line 1090, in render
self.environment.handle_exception()
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\jinja2\environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\jinja2\_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "C:\Users\fr-pa\Desktop\content_aggregator\templates\subscribe\subscribe.html", line 1, in top-level template code
{% extends "layout.html" %}
File "C:\Users\fr-pa\Desktop\content_aggregator\templates\layout.html", line 35, in top-level template code
<div id="content">{% block content %}{% endblock %}</div>
File "C:\Users\fr-pa\Desktop\content_aggregator\templates\subscribe\subscribe.html", line 11, in block "content"
<form method="POST" action="{{ url_for('subscribe') }}"> <!--{{ url_for('subscribe') }} -->
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\helpers.py", line 370, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\app.py", line 2216, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\Lib\site-packages\flask\helpers.py", line 357, in url_for
rv = url_adapter.build(
File "C:\Users\fr-pa\AppData\Local\Programs\Python\Python38\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 'subscribe'. Did you mean 'subscribe.subscribe' instead?
要在蓝图中构建URL,您需要指定蓝图的名称。传递给 url_for()
的端点应采用 <blueprint_name>.<view_function_name>
的形式。在您的情况下,此值为 subscribe.subscibe
.
action="{{ url_for('subscribe.subscribe') }}"
P.S。当前蓝图中使用url_for
时,可以将端点值缩短为.subscribe
.