gpt4 book ai didi

python - 如何让 Flask 方法在有或没有 token 作为路径元素的情况下工作

转载 作者:行者123 更新时间:2023-12-01 04:06:22 26 4
gpt4 key购买 nike

我正在修改 cookiecutter flask 应用程序。我正在尝试关注https://realpython.com/blog/python/handling-email-confirmation-in-flask/添加电子邮件授权。

应用程序会发送一封确认电子邮件。在这种情况下,电子邮件包含一个确认链接,如下所示:

http://127.0.0.1:5000/register/ImNsdWVtYXJpbmUxQG1haWxpbmF0b3IuY29tIg.CajQIA.Pn20l8bkpo3muh1Nk6TSguSoF0I

(其中包含 token 中嵌入的电子邮件“cluemarine1@mailinator.com”。)

这可以与注册函数一起正常工作(在公共(public)蓝图中):

@blueprint.route("/register/<token>", methods=['GET', 'POST'])
def register(token):
form = RegisterForm(request.form, csrf_enabled=False)
email = confirm_token(token)
....

在代码的另一部分中,我想将用户重定向到注册函数,并且我使用了以下行:

return redirect(url_for('public.register'))

在这种情况下,我没有使用 token !结果是:

Werkzeug.routing.BuildError BuildError: ('public.register', {}, None)

我认为这是因为第二种方法没有 token 。处理注册函数中具有或不具有 token 的可能性的最佳方法是什么?

完整回溯:

File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1477, in full_dispatch_reques
rv = self.handle_user_exception(e)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask_debugtoolbar\__init__.py", line 124, in dispatch_request
return view_func(**req.view_args)
File "C:\envs\r2\mini\myflaskapp\views\user.py", line 144, in confirm_email
return redirect(url_for('public.register'))
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\helpers.py", line 312, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\helpers.py", line 305, in url_for
force_external=external)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\werkzeug\routing.py", line 1678, in build
raise BuildError(endpoint, values, method)

最佳答案

是的,可以指定默认值,您可以:

@blueprint.route("/register", defaults={'token': ''}, methods=['GET', 'POST'])
@blueprint.route("/register/<token>", methods=['GET', 'POST'])
def register(token):
# do somethind here
....

这里是文档:URL Route Registrations

关于python - 如何让 Flask 方法在有或没有 token 作为路径元素的情况下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35518163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com