Return 在烧瓶中重定向后的自定义 http 代码
Return custom http code after redirection in flask
我是烧瓶新手。
场景:
提交表单后,我试图重定向到某条路线。
所以我为此使用了烧瓶重定向以及代码参数。
@topics_bp.route('/create_topic/',methods =['GET','POST'] )
def create_topic():
if request.method == 'GET':
#send the form for create topic!
formData = TopicForm()
return render_template('create-topic.html',form = formData)
if request.method == 'POST':
# check the post method and redirect
return redirect(url_for('topic.topics'),code=201)
基本上,我想 return 为创建的记录使用 HTTP 代码 201,然后重定向到预期的路由。
但如果我这样做,它只是 return 一个重定向页面。每次我需要手动点击。
是否有解决 return 201 代码和自动重定向的方法?
提前致谢!
I want to return HTTP code 201 for a record created and then redirect to the intended route.
HTTP 无法做到这一点。重定向本身就是 specific HTTP 30x status code:
In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3
, and a Location
header holding the URL to redirect to.
您 return 201 状态代码 或 您 return HTTP 重定向状态代码之一。你不能两者都做。
flask.redirect()
函数生成有效的 30x 响应(具有所需的 Location
header),documentation for the function 说明支持的重定向状态代码:
Supported codes are 301, 302, 303, 305, 307, and 308. 300 is not supported because it’s not a real redirect and 304 because it’s the answer for a request with a request with defined If-Modified-Since headers.
然而,该函数并不强制执行此操作;状态代码未验证。
您需要在此处区分 浏览器 和其他客户端。 201 Created 响应通常是您从 REST API 到期望在可编程级别进行简单 JSON 或 XML 交互的客户端的 return。另一方面,重定向更常用于表示层的前端。
如果您正在编写 HTML-based front-end,只需 return 重定向。人们不会阅读响应代码,他们会阅读呈现的 HTML 页面。他们不关心也不需要知道用于使浏览器做正确事情的确切 HTTP 代码。鉴于您的路线还包括一个 表单 ,您几乎可以肯定是在为人类而非程序化客户构建网站。
如果您正在构建 REST API,则 return 201 响应,并记录 API 客户端必须根据 Location
header 你包括在内,或者在响应的 body 中的某些内容上。然而,HTML 浏览器不会遵循 Location
header 的 201 响应。
然后我不会为此使用 redirect()
函数,即使它确实允许您使用 201
作为状态代码,因为它总是产生一个(简单的)HTML body 包含您在浏览器中看到的有关自动重定向的文本。
我是烧瓶新手。 场景: 提交表单后,我试图重定向到某条路线。 所以我为此使用了烧瓶重定向以及代码参数。
@topics_bp.route('/create_topic/',methods =['GET','POST'] )
def create_topic():
if request.method == 'GET':
#send the form for create topic!
formData = TopicForm()
return render_template('create-topic.html',form = formData)
if request.method == 'POST':
# check the post method and redirect
return redirect(url_for('topic.topics'),code=201)
基本上,我想 return 为创建的记录使用 HTTP 代码 201,然后重定向到预期的路由。
但如果我这样做,它只是 return 一个重定向页面。每次我需要手动点击。
是否有解决 return 201 代码和自动重定向的方法? 提前致谢!
I want to return HTTP code 201 for a record created and then redirect to the intended route.
HTTP 无法做到这一点。重定向本身就是 specific HTTP 30x status code:
In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with
3
, and aLocation
header holding the URL to redirect to.
您 return 201 状态代码 或 您 return HTTP 重定向状态代码之一。你不能两者都做。
flask.redirect()
函数生成有效的 30x 响应(具有所需的 Location
header),documentation for the function 说明支持的重定向状态代码:
Supported codes are 301, 302, 303, 305, 307, and 308. 300 is not supported because it’s not a real redirect and 304 because it’s the answer for a request with a request with defined If-Modified-Since headers.
然而,该函数并不强制执行此操作;状态代码未验证。
您需要在此处区分 浏览器 和其他客户端。 201 Created 响应通常是您从 REST API 到期望在可编程级别进行简单 JSON 或 XML 交互的客户端的 return。另一方面,重定向更常用于表示层的前端。
如果您正在编写 HTML-based front-end,只需 return 重定向。人们不会阅读响应代码,他们会阅读呈现的 HTML 页面。他们不关心也不需要知道用于使浏览器做正确事情的确切 HTTP 代码。鉴于您的路线还包括一个 表单 ,您几乎可以肯定是在为人类而非程序化客户构建网站。
如果您正在构建 REST API,则 return 201 响应,并记录 API 客户端必须根据 Location
header 你包括在内,或者在响应的 body 中的某些内容上。然而,HTML 浏览器不会遵循 Location
header 的 201 响应。
然后我不会为此使用 redirect()
函数,即使它确实允许您使用 201
作为状态代码,因为它总是产生一个(简单的)HTML body 包含您在浏览器中看到的有关自动重定向的文本。