Django——基于类的视图的 URL 缓存失败

发布于 2024-12-10 19:21:55 字数 881 浏览 0 评论 0原文

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

对于本地测试,我使用默认的缓存后端:

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

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/?

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',
, cache_page(60*60)(DounceListView.as_view(resource=DounceResource)), name='dounces_api'),

I have the following middleware installed.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2024-12-17 19:21:55

更改您的 urlconf 以

urlpatterns = patterns('',
    url(r'^dounces/?

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

, 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

change your urlconf to

urlpatterns = patterns('',
    url(r'^dounces/?

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

, 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

比忠 2024-12-17 19:21:55

另一个答案已经过时了。正确的方法详述如下:

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

现在必须这样做:

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

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'), 
)
野鹿林 2024-12-17 19:21:55

我没有看到任何“django.middleware.cache.UpdateCacheMiddleware”和“django.middleware.cache.FetchFromCacheMiddleware”。我认为没有它它就无法正确缓存。

https://docs.djangoproject.com/en/2.2/topics/cache/< /a>

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文