【发布时间】:2016-10-27 18:48:53
【问题描述】:
我正在使用 python-eve 开发一个 rest-api。我的对象包含一个 url、一个描述和一个图像。用户可以插入或查看产品(用于插入 - 用户只需添加一个 url,并生成描述和图像)。
为了避免重复更新 - 当用户使用 POST 请求插入已经存在的 url 时,我想将他重定向到与该 url 对应的项目的 url。为此,我添加了以下代码:
def pre_post_callback(request, lookup):
prev_object = current_app.data.driver.db['products'].find_one({"url":lookup.values['url']})
if prev_object:
# here I would like to redirect the user to the /product/id of the product corresponding.
app.on_pre_POST += pre_post_callback
但是,当两次添加相同的 url 时,我一直在添加(覆盖)现有项目。
如何将用户从 pre-post 回调挂钩重定向到已经存在的产品?
【问题讨论】:
-
您确定
lookup在这里工作吗?文档是这样说的:Callbacks will receive the resource being requested, the original flask.request object and the current lookup dictionary as arguments (only exception being the on_pre_POST hook which does not provide a lookup argument). -
if prev_object块中的代码是什么 -
if 条件中的代码通常是重定向,但是它不起作用。我的问题是我如何在这个阶段(在那个 if 条件下)返回响应?