Django—如何缓存AJAX结果HTML块

时间:2022-11-19 13:57:43

I have a search page that, by default lists products in a random order. Clicking a product lets you view it, and then there's a 'back' link which takes you back to the product list - this is saved in the random order using {% cache %} <ul of results...</ul> {% endcache %} with memcached. All works fine and well (have even a html comment cached @ h:i:s to check)!

我有一个搜索页面,默认情况下按随机顺序列出产品。单击一个产品可以查看它,然后有一个“back”链接将您带回产品列表——这是使用{% cache %}

    {% endcache %}与memcached。所有的功能都很好(甚至有一个html注释缓存@ h:i:s to check)!
的结果以随机顺序保存的……

The search has an ajax enhanced interface which does all the ajax-type-stuff you'd expect; it also updates the url to reflect exactly the same url that the non js search would do (behind the scenes communicating with this exact url, only with an /ajax/true/ flag in the url iteself).

搜索有一个ajax增强的接口,它可以实现您所期望的所有ajax类型的内容;它还更新url,以反映与非js搜索相同的url(在后台与此url通信,仅在url iteself中使用/ajax/true/ flag)。

When you use the ajax search even once to alter the results, for example set a country name, then click view product, when you click 'back' link the product ordering is back to random (as it has not been cached); the product set is saved though (generated from the url).

当您使用ajax搜索甚至一次来更改结果时,例如设置一个国家名称,然后单击view product,当您单击“返回”链接时,产品订购将返回到random(因为它没有被缓存);但是,产品集是保存的(从url中生成的)。

Is it possible to cache the result block using the django cache as ajax generates it, keeping the order and set exactly the same for when you hit that url again? or should I investigate other avenues?

在ajax生成结果块时,是否可以使用django缓存缓存缓存缓存来缓存结果块,保持顺序并在再次命中该url时设置完全相同?或者我应该研究其他途径吗?

1 个解决方案

#1


1  

Cache the results:

缓存结果:

Example ajax search (with cache):

示例ajax搜索(带有缓存):

https://github.com/tomchuk/django-ajax-tasks/blob/master/example/templates/home.html

https://github.com/tomchuk/django-ajax-tasks/blob/master/example/templates/home.html

https://github.com/tomchuk/django-ajax-tasks/blob/master/ajax_tasks/templatetags/ajax_task.py

https://github.com/tomchuk/django-ajax-tasks/blob/master/ajax_tasks/templatetags/ajax_task.py

From django-ajax-tasks: https://github.com/tomchuk/django-ajax-tasks

从django-ajax-tasks:https://github.com/tomchuk/django-ajax-tasks

#1


1  

Cache the results:

缓存结果:

Example ajax search (with cache):

示例ajax搜索(带有缓存):

https://github.com/tomchuk/django-ajax-tasks/blob/master/example/templates/home.html

https://github.com/tomchuk/django-ajax-tasks/blob/master/example/templates/home.html

https://github.com/tomchuk/django-ajax-tasks/blob/master/ajax_tasks/templatetags/ajax_task.py

https://github.com/tomchuk/django-ajax-tasks/blob/master/ajax_tasks/templatetags/ajax_task.py

From django-ajax-tasks: https://github.com/tomchuk/django-ajax-tasks

从django-ajax-tasks:https://github.com/tomchuk/django-ajax-tasks