理解“post/redirect/get"图案 [英] Understanding the "post/redirect/get" pattern
本文介绍了理解“post/redirect/get"图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难理解发布/重定向/获取"的确切过程.
I am having a very hard time understanding the exact process of "post/redirect/get".
我已经浏览了这个网站和网络几个小时,除了这里是概念"之外,找不到任何其他内容.
I have combed through this site and the web for several hours and cannot find anything other than "here's the concept".
如何理解 post/redirect/get 模式?
How to understand the post/redirect/get pattern?
推荐答案
正如您从 research, POST
-redirect-GET
看起来像这样:
As you may know from your research, POST
-redirect-GET
looks like this:
- 客户得到一个带有表单的页面.
- 表单
POST
发送到服务器. - 服务器执行操作,然后重定向到另一个页面.
- 客户端遵循重定向.
例如,假设我们有这样的网站结构:
For example, say we have this structure of the website:
/posts
(显示帖子列表和添加帖子"链接)/
(查看特定帖子)/create
(如果使用GET
方法请求,则返回一个发布给自身的表单;如果它是一个POST
请求,则创建发布并重定向到/
端点)
/posts
(shows a list of posts and a link to "add post")/<id>
(view a particular post)/create
(if requested with theGET
method, returns a form posting to itself; if it's aPOST
request, creates the post and redirects to the/<id>
endpoint)
/posts
本身与这个特定的模式并不真正相关,所以我会忽略它./posts
itself isn't really relevant to this particular pattern, so I'll leave it out./posts/
可以这样实现:- 在数据库中查找具有该 ID 的帖子.
- 使用该帖子的内容呈现模板.
/posts/create
可以这样实现:- 如果请求是
GET
请求:- 显示一个空表单,目标设置为自身,方法设置为
POST
.
- 验证字段.
- 如果存在无效字段,请再次显示表单并指出错误.
- 否则,如果所有字段都有效:
- 将帖子添加到数据库中.
- 重定向到
/posts/
(其中
从对数据库的调用返回)
- Validate the fields.
- If there are invalid fields, show the form again with errors indicated.
- Otherwise, if all fields are valid:
- Add the post to the database.
- Redirect to
/posts/<id>
(where<id>
is returned from the call to the database)
这篇关于理解“post/redirect/get"图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 显示一个空表单,目标设置为自身,方法设置为
查看全文