缓存中间件必须腌制Response对象,但有时候它会失败:
[...]/.virtualenvs/project/lib/python2.7/site-packages/django/template/response.py in __getstate__
Ensures that the object can't be pickled before it has been
rendered, and that the pickled state only includes rendered
data, not the data used to construct the response.
"""
obj_dict = self.__dict__.copy()
if not self._is_rendered:
raise ContentNotRenderedError('The response content must be rendered before it can be pickled.')
del obj_dict['template_name'] ...
KeyError: template_name
当地的变种是:
Variable Value
self
u'Content-Language: fr\nExpires: Thu, 23 Jun 2011 17:40:18 GMT\nVary: Accept-Language, Cookie\nLast-Modified: Thu, 23 Jun 2011 11:40:18 GMT\nCache-Control: max-age=21600\nContent-Type: text/html; charset=utf...'
obj_dict
{'_charset': 'utf-8',
'_container': [u'\n\n\n\n\n<li>\n <a href="/video/42506/outdoor-demo">\n <img src="http://str2.site.com/4/2/5/0/6/42506/screenshots_80x80/4.jpg" />\n <h3>Outdoor demo </h3>\n <p>\n <st...'],
'_headers': {'cache-control': ['Cache-Control', 'max-age=21600'],
'content-language': ['Content-Language', 'fr'],
'content-type': ['Content-Type', 'text/html; charset=utf-8'],
'expires': ['Expires', 'Thu, 23 Jun 2011 17:40:18 GMT'],
'last-modified': ['Last-Modified',
'Thu, 23 Jun 2011 11:40:18 GMT'],
'vary': ['Vary', 'Accept-Language, Cookie']},
'_is_rendered': True,
'_is_string': True,
'cookies': {}}
del obj_dict['context_data']
del obj_dict['_post_render_callbacks']
return obj_dict
def resolve_template(self, template):
这个问题并不是一直存在,而且我不确定,但我认为它只与一种用AJAX调用的视图有关。以下是一个示例视图:
class CategoriesView(ChunkTemplateResponseMixin, generic.ListView):
"""
List categories
"""
template_name = 'app/categories.html'
context_object_name = 'categories'
paginate_by = 10
model = Tag
chunk_template_name = 'app/categories_chunk.html'
chunk_marker = 'chunk'
def get_queryset(self):
"""
Return the categ by usage, removing categs with no videos on the
current site
"""
categs = []
videos = Video.on_site.all()
for tag in Video.tags_set.usage():
if Video.tagged.with_all(tag, videos).count() > 25:
categs.append(tag)
return categs
这显然是所有观点的共同点:
class ChunkTemplateResponseMixin(TemplateResponseMixin):
"""
Return a different template if the request pass a parameter.
If all request are in ajax and you still want to differentiate
page loaded entirely from chunk, set chunk marker to a string
that enable the alternate template rendering when found in
GET or POST.
eg: /categories => normal template
/categories?chunk=True => chunk template
"""
chunk_marker = None
chunk_template_name = None
def is_chunk(self):
"""
Check if the template chunk should be rendered instead of
the original one.
"""
if self.chunk_marker:
return self.chunk_marker in self.request.REQUEST
return self.request.is_ajax()
def render_to_response(self, *args, **kwargs):
if self.is_chunk():
self.template_name = self.chunk_template_name
return super(ChunkTemplateResponseMixin,
self).render_to_response(*args, **kwargs)
我猜测有些东西与它的TemplateResponseMixin相关
但我确实为孩子设置了template_name
。 enter code here