Kit*_* Ho 5 python flask asp.net-web-api
我是网络开发的新手。我现在正在使用提交按钮实现一个简单的创建/编辑用户表单。
我想知道实现这一点的更好做法。
我已经定义了这种web api
URL Method Description
/users/ GET Gives a list of all users
/users/ POST Creates a new user
/users/<id> GET Shows a single user
/users/<id> PUT Updates a single user
/users/<id> DELETE Deletes a single user
Run Code Online (Sandbox Code Playgroud)
我的第一种方法:
我创建了两个新的“/user/add”和“/usr/edit”函数,类似于
app.route("/users/edit")
def edit_user(){
....
....
call the internal api /user/ with a "put" method
....
render_template("edit.html")
Run Code Online (Sandbox Code Playgroud)
当我点击提交按钮时,我调用上面的内部 api /users/ ,method=PUT,并呈现最终模板。
我的第二种方法:
在我的内部 api /user/ 中,我尝试读取 http 标头以查看是否需要 html 模板或 json 文本并返回给用户
再说一次,当我想创建一个编辑表单时,我不是调用 /user/edit ,而是调用 /user/,使用 PUT 方法
def put(self, id):
//see the header of that request
if header == html
render_template("edit.html", .....)
if header == json
update the record
Run Code Online (Sandbox Code Playgroud)
#
我的问题,基本上,我不知道制作表单是否需要“/user/add”“/user/edit”路由,或者我们可以简单地将不同的“post”或“put”嵌入到/user/api中“ 方法。
这个想法来自这里,来自flask,pluggable view,我想知道如何更好地实现
或者这是一个更好的方法吗???
非常感谢。
我会选择你的第一种方法,因为你的 URL 清晰且合乎逻辑。此外,您还可以将前端(带有表单的网站)和后端(API)分开,这在测试中非常有帮助。普通的 Web 浏览器仅向网站发出 GET 和 POST 请求,因此很难通过 PUT 请求为用户呈现模板,因为用户通常无法启动 put 请求。
| 归档时间: |
|
| 查看次数: |
371 次 |
| 最近记录: |