[英]How to add allow response header to all django views?
如何將指定允許的 HTTP 方法的“允許”header 添加到所有 Django 視圖的響應標頭中? 為了更清楚,這是一個例子:
允許:GET、HEAD、OPTIONS
對於基於類的視圖,您可以添加一個 mixin 來確定這一點。 的確:
class AllowViewMixin:
def dispatch(self, *args, **kwargs):
response = super().dispatch(*args, **kwargs)
response.headers['Allow'] = ', '.join(self._allowed_methods())
return response
然后你可以在基於類的視圖中混合它,例如ListView
:
class MyListView(AllowViewMixin, ListView):
# …
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.