对 Django 表单 POST 请求的结果进行分页 [英] Paginating the results of a Django forms POST request

查看:58
本文介绍了对 Django 表单 POST 请求的结果进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Django 表单通过 POST 进行过滤/分面搜索,并且我想使用 Django 的分页器类来组织结果.在各个页面之间传递客户端时如何保留原始请求?换句话说,一旦我将另一个页面的 GET 请求传递回我的视图,我似乎就丢失了 POST 数据.我已经看到一些建议使用 AJAX 仅刷新页面的结果块,但我想知道是否有 Django 原生机制可以执行此操作.

I'm using Django Forms to do a filtered/faceted search via POST, and I would like to Django's paginator class to organize the results. How do I preserve the original request when passing the client between the various pages? In other words, it seems that I lose the POST data as soon as I pass the GET request for another page back to my views. I've seen some recommendations to use AJAX to refresh only the results block of the page, but I'm wondering if there is a Django-native mechanism for doing this.

谢谢.

推荐答案

如果您想在以后的请求中访问存储数据,则必须将其存储在某处.Django 提供了几种存档方式:

If you want to access the store data in later request, you would have to store it somewhere. Django provides several ways to archive this:

1) 您可以使用 sessions 来存储查询:每个访问您网站的访问者都会得到一个空的会话对象,您可以在这个对象中存储您想要的任何内容,它的作用类似于 dict.缺点:单个访问者不能同时使用分页进行多次搜索.

1) You can use sessions to store the query: Every visitor who visits your site will get an empty session object and you can store whatever you want inside this object, which acts like a dict. Drawback: A single visitor can't do multiple searches with pagination concurrently.

2) 使用cookies:如果您设置了一个存储在客户端的cookie,浏览器会将cookie 的数据附加到您可以访问它的每个请求中.Cookie 对服务器更友好,因为您不需要在服务器上为它们设置会话管理器,但存储在 cookie 中的数据对客户端是可见的(并且可编辑).缺点:和以前一样.

2) Use cookies: If you set a cookie which is stored on the client side, the browser will append the data of the cookie to each request where you can access it. Cookies are more server friendly, because you don't need a session manager for them on the server, but the data stored in cookies is visible (and editable) to the client. Drawback: same as before.

3) 使用隐藏字段:您可以在搜索结果页面上添加带有一些隐藏字段的表单,并将查询存储在其中.然后,每当您提交表单时,客户端都会重新发送查询.缺点:您必须在页面上使用带有提交按钮的表单来进行分页(简单的链接不起作用).

3) Use hidden fields: You can add a form with some hidden fields on your search-result page and store the query inside them. Then, the client will resend the query whenever you submit the form. Drawback: You must use a form with submit buttons for the pagination on your page (simple links wont work).

4) 创建包含查询的链接: 除了使用 POST,您还可以使用 GET.例如,你可以有一个像 "/search/hello+world/?order=votes" 这样的链接和像 "/search/hello+world/2/?order 这样的分页链接"-投票".然后可以轻松地从 URL 中检索查询.缺点:通过 GET 可以发送的最大数据量是有限的(但对于简单的搜索来说这应该不是问题).

4) Create Links which contain the query: Instead of using POST, you can also use GET. For example, you could have a link like "/search/hello+world/?order=votes" and "paginated links" like "/search/hello+world/2/?order-votes". Then the query can be easily retrieved from the URL. Drawback: The maximum amount of data you can send via GET is limited (But that shouldn't be a problem for a simple search).

5) 使用组合:您可能希望将所有数据存储在会话或数据库中,并通过生成的密钥访问它们,您可以将其放入 URL.URL 可能看起来像/search/029af239ccd23/2"(对于第二页),您可以使用该密钥访问您之前存储的大量数据.这消除了解决方案 1 和解决方案 4 的缺点.新的缺点:很多工作:)

5) Use a combination: You might want to store all the data in a session or a database and access them via a generated key which you can put in the URL. URLs might then look like "/search/029af239ccd23/2" (for the 2nd page) and you can use the key to access a huge amount of data which you have stored before. This eliminates the drawback of solution 1 as well as that of solution 4. New drawback: much work :)

6) 使用 AJAX: 使用 ajax,您可以将数据存储在客户端的一些 js 变量中,然后可以将其传递给其他请求.由于 ajax 只会更新您的结果列表,因此变量不会丢失.

6) Use AJAX: With ajax you can store the data inside some js-variables on the client side, which can then passed to the other requests. And since ajax will only update your result list, the variables aren't getting lost.

这篇关于对 Django 表单 POST 请求的结果进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆