我想在我的POST(JSON)请求中添加另一个字段。键值对必须为“请求ID”。我得出结论,最好的方法是使用中间件生成随机的请求ID,并将其注入请求对象中。我编写了自定义中间件来实现这一点。但当我尝试访问端点时,遇到了错误。
我尝试通过互联网搜索解决此错误,但没有找到解决方案。
class SimpleMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
req = json.loads(request.body)
req_id = random.randint(0,1000000)
req["req_id"]=req_id
response = self.get_response(req)
# Code to be executed for each request/response after
# the view is called.
return response
我遇到的错误是'dict' object has no attribute 'is_ajax'。 请帮我修复此问题,如果有更简单、更好的实现方法,请告诉我。