Django:行在被删除后仍然显示 - 因为缓存?

发布于 2024-12-22 22:15:52 字数 1066 浏览 1 评论 0原文

我编写了一个使用 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

I've written a Django app that uses DataTables. The problem is when I delete a row from the table it's still displayed in the table when running against nginx/gunicorn. However, it works correctly when I'm running against the Django test server. So if I start a server with this command line:

python manage.py runserver 192.168.0.1:8000

everything works fine. That is, I delete the row, the table refreshes, and the deleted row is not displayed.

This is a summary of the HTTP calls:

// 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)

The problem is when I use nginx/gunicorn the second GET call returns the same list as the first GET including the row that I know has been deleted from the backend database.

I'm not sure it's a caching problem either because this is the response header I get from the first 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江南月 2024-12-29 22:15:52

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

$.ajaxSetup({ cache: false});

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

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

The problem can be solved also by sending an added parameter to the server so that the browser doesn't cache the call. With jQuery you can simply use:

$.ajaxSetup({ cache: false});

Otherwise you must creat manually the parameter. Usually you create a timestamp

var nocache = new Date().getTime();
//send nocache as a parameter so that the browser thinks it's a new call
命硬 2024-12-29 22:15:52

您可以使用 Django 的 add_never_cache_headers 或 never_cache 装饰器来告诉浏览器不要缓存给定的请求。文档位于此处。我认为这是比在 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):
            (...)

You can use Django's add_never_cache_headers or never_cache decorator to tell the browser not to cache given request. Documentation is here. I thinks it's cleaner solution than forcing the cache off in the 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):
            (...)
假装爱人 2024-12-29 22:15:52

试试这个

oTable.fnDeleteRow( anSelected, null, true );

,顺便问一下你使用什么版本的数据表?

try this

oTable.fnDeleteRow( anSelected, null, true );

and b.t.w what version of datatables do you use?

为你鎻心 2024-12-29 22:15:52

我通过改变解决了这个问题

获取/myapp/get_list

POST /myapp/get_list

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

I fixed the problem by changing

GET /myapp/get_list

to

POST /myapp/get_list

After doing this, I no longer get a cached response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文