![](/img/trans.png)
[英]what's differece between the django's cache ?the The per-site cache,The per-view cache,Specifying per-view cache and Template fragment caching
[英]Django: use per-view cache in the URLconf?
我正在使用Django 1.8,我想開始使用Django的文件系統緩存,並在URLconf中使用按視圖緩存 。
現在是我的urls.py
:
urlpatterns = patterns(
'',
url(r'^api/1.0/spending$',
'frontend.views.views_api_spending.total_spending_on_substance',
name='total_spending'),
這是我正在嘗試的:
urlpatterns = patterns(
'',
url(r'^api/1.0/spending$',
cache_page(60 * 15)('frontend.views.views_api_spending.total_spending_on_substance'),
name='total_spending'),
但我收到一個錯誤: TypeError at /api/1.0/spending: 'str' object is not callable
。
如果刪除引號, NameError at /api/1.0/spending: name 'frontend' is not defined
得到: NameError at /api/1.0/spending: name 'frontend' is not defined
。
如何調整我的url文件以開始使用按視圖緩存?
為此,您需要在urls.py
導入total_spending_on_substance
視圖函數,然后將total_spending_on_substance
視圖函數傳遞給cache_page
裝飾器而不是字符串 。
cache_page
是一個裝飾器,它需要一個view函數。 因此,在URLconf中引用視圖函數total_spending_on_substance
時,請使用cache_page total_spending_on_substance
進行包裝。
您可以執行以下操作:
from frontend.views.views_api_spending import total_spending_on_substance # import the view function
urlpatterns = patterns(
'',
url(r'^api/1.0/spending$',
cache_page(60 * 15)(total_spending_on_substance), # pass function instead of string
name='total_spending'),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.