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

查看:265
本文介绍了分页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)您可以使用会话来存储查询:访问您的站点的每个访问者都将获得一个空的会话对象,您可以在此对象中存储任何所需内容,这就像一个字典。缺点:单个访问者不能同时进行多次分页搜索。

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)使用Cookie:如果您设置了存储的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和paginated links(如)/搜索链接/你好+世界/ 2 /?订单内容。然后可以从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天全站免登陆