2011-12-23 49 views
2

我寫過一個使用DataTables的Django應用程序。問題是,當我從表中刪除一行時,它仍然顯示在表中,當對nginx/gunicorn運行時。但是,當我運行在Django測試服務器上時,它正常工作。所以如果我用這個命令行啓動服務器: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 
+0

如何刪除該行?使用什麼命令? – Daniel

+0

我使用oTable.fnDeleteRow(anSelected);在客戶端。我也通過ajax發送POST請求到服務器(工作)。 – FunLovinCoder

回答

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 
0

試試這個

oTable.fnDeleteRow(anSelected, null, true); 

,並順便說一句什麼數據表做的版本你用?

+0

不幸的是,這並沒有幫助。我使用的是1.8.1版本。謝謝。 – FunLovinCoder

0

我改變

GET/MYAPP/get_list

POST/MYAPP/get_list

這樣做後固定的問題,我不再獲得緩存響應。

1

您可以使用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): 
      (...) 
相關問題