我正在尝试修改ListView
的响应标头,以解决看似缓存的问题。这是我正在尝试的:
def get(self, request, *args, **kwargs):
context = super(MapListView, self.get_context_data(*args, **kwargs) # Errors here
response = super(MapListView, self).render_to_response(context, **kwargs)
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
我也尝试过:
def get(self, request, *args, **kwargs):
context = self.get_context_data() # Errors here
[. . . ]
在任何一种情况下,都抛出AttributeError
:
'MapListView" object has no attribute 'object_list'
这显然发生在get_context_data()
来自MultipleObjectMixin
的这一行:
queryset = kwargs.pop('object_list', self.object_list)
我应该做些什么?如何更改ListView
的回复标题?
供参考,这里是whole get_context_data()
definition。
更多参考资料,这是我的全部观点:
class MapContactClientListView(ListView):
model = Map # Note: This isn't the real name, so it's not a problem with this.
cursor = connections["default"].cursor()
cursor.execute("""SELECT map.*
FROM map
INNER JOIN profile
ON (map.profile_id = profile.id)
ORDER BY profile.name""")
queryset = dictfetchall(cursor)
paginate_by = 20
template_name = 'onboardingWebApp/map_list.html'
def get(self, request, *args, **kwargs):
context = super(MapListView, self.get_context_data(*args, **kwargs)
response = super(MapListView, self).render_to_response(context, **kwargs)
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
答案 0 :(得分:1)
在def get(self, request, *args, **kwargs):
def get(self, request, *args, **kwargs):
response = super(MapListView, self).get(request,*args,**kwargs)
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
对super()。get()的调用将正确设置object_list,但它依赖于get_queryset。我不相信你已经正确设置了你的查询集(因为你在类定义中动态设置它),所以我将其更改为:
def get_queryset(self):
cursor = connections["default"].cursor()
cursor.execute("""SELECT map.*
FROM map
INNER JOIN profile
ON (map.profile_id = profile.id)
ORDER BY profile.name""")
return dictfetchall(cursor)