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

时间:2023-01-27 18:18:44

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:

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

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:

这是对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)

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.

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

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

4 个解决方案

#1


1  

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:

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

$.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

#2


1  

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.

您可以使用Django的add_never_cache_header或never_cache decorator来告诉浏览器不要缓存给定的请求。文档在这里。我认为这比在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


0  

try this

试试这个

oTable.fnDeleteRow( anSelected, null, true );

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

和b.t。你们使用什么版本的数据?

#4


0  

I fixed the problem by changing

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

GET /myapp/get_list

获得/ myapp / get_list

to

POST /myapp/get_list

POST / myapp / get_list

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

这样做之后,我将不再获得缓存响应。

#1


1  

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:

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

$.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

#2


1  

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.

您可以使用Django的add_never_cache_header或never_cache decorator来告诉浏览器不要缓存给定的请求。文档在这里。我认为这比在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


0  

try this

试试这个

oTable.fnDeleteRow( anSelected, null, true );

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

和b.t。你们使用什么版本的数据?

#4


0  

I fixed the problem by changing

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

GET /myapp/get_list

获得/ myapp / get_list

to

POST /myapp/get_list

POST / myapp / get_list

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

这样做之后,我将不再获得缓存响应。