基于类的视图中的get_queryset方法的每视图缓存(Django应用程序)

时间:2015-10-31 13:22:58

标签: python django

TL; DR:

我似乎无法将每次浏览缓存应用于基于类的视图的get_queryset方法。有没有其他方法可以用来将缓存应用于基于类的视图方法?或者,根本没有办法吗?

目前,我得到的错误是:'View'对象没有属性'method'。如果我在视图顶部写相同的装饰器,我得到:'函数'对象没有属性'as_view'(下面的回溯)。

详细信息:

以下是观点:

#@cache_page(20)
class OnlineView(ListView):
    template_name = "online.html"
    paginate_by = 75

    #@cache_page(20)   
    def get_queryset(self):
        users = Session.objects.filter(last_activity__gte=(timezone.now()-timedelta(minutes=5))).only('user').distinct('user')
        return users

这是我在视图顶部写入缓存标记时获得的追溯

Traceback (most recent call last):
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 92, in inner_run
    self.validate(display_num_errors=True)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/management/base.py", line 280, in validate
    num_errors = get_validation_errors(s, app)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/management/validation.py", line 35, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/loading.py", line 166, in get_app_errors
    self._populate()
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/loading.py", line 75, in _populate
    self.load_app(app_name)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/loading.py", line 96, in load_app
    models = import_module('.models', app_name)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/debug_toolbar/models.py", line 63, in <module>
    patch_root_urlconf()
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/debug_toolbar/models.py", line 51, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 467, in reverse
    app_list = resolver.app_dict[ns]
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 311, in app_dict
    self._populate()
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 263, in _populate
    for pattern in reversed(self.url_patterns):
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 347, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/hassan/Desktop/unconnectedredditpk/unconnectedreddit/urls.py", line 26, in <module>
    url(r'^online/$', auth(OnlineView.as_view()), name='online'),
AttributeError: 'function' object has no attribute 'as_view'

最后,这是我在 urls.py 中的相关内容:

url(r'^online/$', auth(OnlineView.as_view()), name='online'),

1 个答案:

答案 0 :(得分:0)

通过adding the caching in your URL config

,您可以更轻松地实现这一目标
from django.views.decorators.cache import cache_page

url(r'^online/$', cache_page(20)(auth(OnlineView.as_view())), name='online'),

但是我注意到你在同一个视图上有一个auth()装饰器。我不确定你想要缓存需要身份验证的视图......