![](/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.