请解释我提到的代码行
Please explain the line of code I Mentioned
这是我在书上看到的mixin
from django.core.cache import caches
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie
class CachePageVaryOnCookieMixin:
cache_name = 'default'
@classmethod
def get_timeout(cls):
if hasattr(cls, 'timeout'):
return cls.timeout
cache = caches[cls.cache_name]
return cache.default_timeout
@classmethod
def as_view(cls, *args, **kwargs):
view = super().as_view(*args, **kwargs)
view = vary_on_cookie(view)
view = cache_page(timeout = cls.get_timeout(), cache = cls.cache_name)(view)
return view
在as_view()中view = cache_page(timeout = cls.get_timeout(), cache = cls.cache_name)(view)
(view) 最后有什么用。是类型转换吗?
cache_page
是一个函数,returns 是另一个接受视图作为参数的函数。这也可以像这样分成两部分:
callable_func = cache_page(timeout = cls.get_timeout(), cache = cls.cache_name)
return callable_func(view)
这是我在书上看到的mixin
from django.core.cache import caches
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie
class CachePageVaryOnCookieMixin:
cache_name = 'default'
@classmethod
def get_timeout(cls):
if hasattr(cls, 'timeout'):
return cls.timeout
cache = caches[cls.cache_name]
return cache.default_timeout
@classmethod
def as_view(cls, *args, **kwargs):
view = super().as_view(*args, **kwargs)
view = vary_on_cookie(view)
view = cache_page(timeout = cls.get_timeout(), cache = cls.cache_name)(view)
return view
在as_view()中view = cache_page(timeout = cls.get_timeout(), cache = cls.cache_name)(view)
(view) 最后有什么用。是类型转换吗?
cache_page
是一个函数,returns 是另一个接受视图作为参数的函数。这也可以像这样分成两部分:
callable_func = cache_page(timeout = cls.get_timeout(), cache = cls.cache_name)
return callable_func(view)