了解“发布/重定向/获取” [英] Understanding "post/redirect/get"
问题描述
我很难理解post / redirect / get的确切过程。我已经梳理了这个网站和网络了好几个小时,除了这里是概念以外,什么也找不到。它是如此简单,它不需要一步一步的教程?有人知道我可以用一个好的网站或书籍来找到一个例子或教程吗?
研究, POST
-redirect - GET
看起来像这样:
POST
到服务器。
例如,假设我们有这样的网站结构:
/ posts
(显示帖子列表和指向添加帖子的链接)
/< id>
(查看特定帖子)
/ create
GET 方法,返回表单发布到自身;如果它是 POS T
请求,创建帖子并重定向到 /< id>
终端)
/ posts
本身并不与这个特定模式相关,所以我'/ b>
/ posts /< id>
可能会像这样执行:
- 使用该ID在数据库中查找帖子。 使用该帖子的内容呈现模板。
/ posts / create
可以这样实现:
- 如果请求是
GET
请求:
- 显示一个空目标,目标设置为自身,方法设置为
POST
。
- 显示一个空目标,目标设置为自身,方法设置为
- 如果请求是
POST
请求:
- 验证
- 如果所有字段都是有效的,那么
- 如果有无效的字段,则再次显示带有错误的表单。 b
- 将该信息添加到数据库中。 重定向至
/ posts /< id> (其中 < id>
是从调用数据库返回的)
- 验证
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". Is it so simple that it requires no step-by-step tutorial? Does anyone know of a good website or book I could use to find an example or tutorial?
As you may know from your research, POST
-redirect-GET
looks like this:
- The client gets a page with a form.
- The form
POST
s to the server. - The server performs the action, and then redirects to another page.
- The client follows the redirect.
For example, say we have this structure of the website:
/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
itself isn't really relevant to this particular pattern, so I'll leave it out.
/posts/<id>
might be implemented like this:
- Find the post with that ID in the database.
- Render a template with the content of that post.
/posts/create
might be implemented like this:
- If the request is a
GET
request:- Show an empty form with the target set to itself and the method set to
POST
.
- Show an empty form with the target set to itself and the method set to
- If the request is a
POST
request:- 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)
这篇关于了解“发布/重定向/获取”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!