Django - 基于类的视图的URL缓存失败 [英] Django--URL Caching Failing for Class Based Views

查看:130
本文介绍了Django - 基于类的视图的URL缓存失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Django Rest框架之上构建了一个RESTful API。 API的URL conf由基于类的视图组成。



我想缓存这些视图,但是,以下是失败。任何关于为什么可能会改变它的想法?

  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' )

我安装了以下中间件。

 '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',

为了本地测试, m使用默认缓存后端:

 'BACKEND':'d jango.core.cache.backends.locmem.LocMemCache',


解决方案

p>将您的urlconf更改为

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

还可以看到 https://docs.djangoproject.com/en/1.7/topics/cache/#specifying-per-view-cache-in-the-urlconf


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',

解决方案

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

这篇关于Django - 基于类的视图的URL缓存失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆