如果不想建立新的middleware 就在默认的或新增的AuthenticationMiddleware下面新增process_response函数(视图函数执行后的一些操作)
以下为部分代码
def process_response(self, request, response):
try:
ip = request.META.get('HTTP_ORIGIN')
response["Access-Control-Allow-Origin"] = ip
response["Access-Control-Allow-Credentials"] = 'true'
response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
response["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept"
response["Access-Control-Max-Age"] = "1728000"
response["Access-Control-Expose-Headers"] = 'Date'
except Exception as e:
print(e)
来源:CSDN
作者:努力w努力w努力
链接:https://blog.csdn.net/weixin_42904196/article/details/103608471