为了与 slack 交互,服务器需要能够基于一些加密散列验证请求。如果此检查返回 false,则服务器应以 400 响应。将其作为 mixin 执行似乎是明智的:
class SlackValidationMixin:
def dispatch(self, request, *args, **kwargs):
if validate_slack_request(request):
return super().dispatch(request, *args, **kwargs)
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
这给出了错误“accepted_renderer not set on Response”基于 SO question,我添加了以下内容:
class SlackValidationMixin:
def dispatch(self, request, *args, **kwargs):
if validate_slack_request(request):
return super().dispatch(request, *args, **kwargs)
else:
response = Response(status=status.HTTP_400_BAD_REQUEST)
response.accepted_renderer = JSONRenderer
response.accepted_media_type = "application/json"
response.renderer_context = {}
return response
但这给出了错误:AttributeError: 'NoneType' object has no attribute 'get_indent'
为什么它需要一个accepted_renderer,因为它只响应一个HTTP状态码,没有额外的数据?解决这个问题的最简单方法是什么?
以下建议回答使 EmptyResponse 对象继承自 Response:
Traceback (most recent call last):
File "path/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "path/lib/python3.8/site-packages/django/utils/deprecation.py", line 96, in __call__
response = self.process_response(request, response)
File "path/lib/python3.8/site-packages/django/middleware/common.py", line 106, in process_response
if response.status_code == 404:
AttributeError: 'dict' object has no attribute 'status_code'