Django--URL Caching Failing for Class Based Views

孤者浪人 提交于 2020-02-05 21:30:13

问题


I've built a RESTful API on top of the Django Rest Framework. The URL conf for the API is composed of class based views.

I would like to cache these views, however, the following is failing. Any thoughts on why that might be and how I could change it?

   from django.views.decorators.cache import cache_page

   urlpatterns = patterns('',
   url(r'^dounces/?$', cache_page(60*60)(DounceListView.as_view(resource=DounceResource)), name='dounces_api'),

I have the following middleware installed.

'django.middleware.gzip.GZipMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'django.contrib.redirects.middleware.RedirectFallbackMiddleware',

AND for local testing, i'm using the default caching backend:

 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',

回答1:


change your urlconf to

urlpatterns = patterns('',
    url(r'^dounces/?$', cache_page(60*60)(DounceListView.as_view(resource=DounceResource)), name='dounces_api'),
)

also see https://docs.djangoproject.com/en/1.7/topics/cache/#specifying-per-view-cache-in-the-urlconf




回答2:


The other answer is out of date. The correct way is detailed here:

https://docs.djangoproject.com/en/1.6/topics/cache/#specifying-per-view-cache-in-the-urlconf

It has to now be done this way:

urlpatterns = patterns('',
    url(r'^dounces/?$', cache_page(60*60)(DounceListView.as_view(resource=DounceResource)), name='dounces_api'), 
)



回答3:


I dont see any "django.middleware.cache.UpdateCacheMiddleware" and "django.middleware.cache.FetchFromCacheMiddleware". I dont think it can cache properly without it.

https://docs.djangoproject.com/en/2.2/topics/cache/



来源:https://stackoverflow.com/questions/7841772/django-url-caching-failing-for-class-based-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!