【问题标题】:Django: row still displayed after being deleted - because of caching?Django:行被删除后仍然显示 - 因为缓存?
【发布时间】:2012-01-26 23:22:05
【问题描述】:

我编写了一个使用 DataTables 的 Django 应用程序。问题是当我从表中删除一行时,在针对 nginx/gunicorn 运行时它仍然显示在表中。但是,当我针对 Django 测试服务器运行时,它可以正常工作。所以如果我用这个命令行启动一个服务器:

python manage.py runserver 192.168.0.1:8000

一切正常。也就是我删除行,表格刷新,删除的行不显示了。

这是 HTTP 调用的摘要:

// An initial GET command to populate the table
GET /myapp/get_list (returns a list to display)

// I now select a row and delete it which causes this POST to fire
POST /myapp/delete (deletes a row from the list)

// After the POST the code automatically follows up with a GET to refresh the table
GET /myapp/get_list (returns a list to display)

问题是当我使用 nginx/gunicorn 时,第二个 GET 调用返回与第一个 GET 相同的列表,包括我知道已从后端数据库中删除的行。

我也不确定这是缓存问题,因为这是我从第一个 GET 获得的响应标头:

Date    Fri, 23 Dec 2011 15:04:16 GMT
Last-Modified   Fri, 23 Dec 2011 15:04:16 GMT
Server  nginx/0.7.65
Vary    Cookie
Content-Type    application/javascript
Cache-Control   max-age=0
Expires Fri, 23 Dec 2011 15:04:16 GMT

【问题讨论】:

  • 如何删除行?使用什么命令?
  • 我使用 oTable.fnDeleteRow(anSelected);在客户端。我还通过 ajax 向服务器发送一个 POST 请求(有效)。

标签: django http nginx datatables gunicorn


【解决方案1】:

这个问题也可以通过向服务器发送一个附加参数来解决,这样浏览器就不会缓存调用。使用 jQuery,您可以简单地使用:

$.ajaxSetup({ cache: false});

否则您必须手动创建参数。通常你创建一个时间戳

var nocache = new Date().getTime();
//send nocache as a parameter so that the browser thinks it's a new call

【讨论】:

    【解决方案2】:

    您可以使用 Django 的 add_never_cache_headers 或 never_cache 装饰器告诉浏览器不要缓存给定的请求。文档是here。我认为它比在 javascript 中强制缓存更清洁。

        from django.utils.cache import add_never_cache_headers
    
        def your_view(request):
            (...)
            add_never_cache_headers(response)
            return response
    
        from django.views.decorators.cache import never_cache
    
        @never_cache
        def your_other_view(request):
                (...)
    

    【讨论】:

      【解决方案3】:

      试试这个

      oTable.fnDeleteRow( anSelected, null, true );
      

      顺便说一句,您使用什么版本的数据表?

      【讨论】:

      • 不幸的是,这没有帮助。我使用的是 1.8.1 版。谢谢。
      【解决方案4】:

      我通过更改解决了问题

      GET /myapp/get_list

      POST /myapp/get_list

      执行此操作后,我不再获得缓存响应。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-09
        • 2022-01-10
        • 2018-11-28
        • 1970-01-01
        • 2017-03-28
        • 1970-01-01
        相关资源
        最近更新 更多