Django前后端跨域问题解决

时间:2024-11-17 10:26:04

自定义中间件

在utils的cors下配置

from django.utils.deprecation import MiddlewareMixin


class CorsMiddleware(MiddlewareMixin):

    def process_response(self, request, response):
        response['Access-Control-Allow-Origin'] = '*'
        response['Access-Control-Allow-Headers'] = '*'
        response['Access-Control-Allow-Methods'] = '*'
        return response

在settings的中间件配置路径

MIDDLEWARE = [
	...
	...
    "utils.cors.CorsMiddleware"
]

最后跨域解决:
在这里插入图片描述