python - 如何更改/添加 Django 的 ListView 的响应 header ?

标签 python django listview httpresponse

我正在尝试修改 ListView 的响应 header ,以解决似乎是缓存的问题。这是我正在尝试的:

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'

这显然发生在 MultipleObjectMixinget_context_data() 行上:

queryset = kwargs.pop('object_list', self.object_list)

我应该采取什么不同的做法?如何更改 ListView 的响应 header ?


作为引用,这里是 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

最佳答案

在你的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)

关于python - 如何更改/添加 Django 的 ListView 的响应 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26027255/

相关文章:

android - 在 ViewPager android 中处理 listview 的点击事件

c# - 在 Xamarin.Forms 中的分组 ListView 中搜索

python - 如何从 kivy 文件 (.kv) 访问不同类的 id/widget?

python - 在 python 中的循环中编写循环的更简洁的方法

python - 带有brew的OS X上的virtualenv中的PyGame?

python - 在 Django 管理器中,为什么使用 self.get_query_set().get(kwarg=val) 而不是 self.get(kwarg=val) ?

django - 在另一个下拉列表上进行选择时,如何在Django管理员中过滤下拉列表

python - doc2vec的余弦相似度不准确

python - django 静态文件 - 不起作用

listview - 在 Xamarin 表单中将 ReactiveList<T> 绑定(bind)到 ListView