django跨域问题解决方案

使用第三方包解决

  • 安装:pip3 install django-cors-headers
  • INSTALLED_APPS中添加corsheaders
  • MIDDLEWARE中添加'corsheaders.middleware.CorsMiddleware'
  • settings文件添加
    CORS_ALLOW_CREDENTIALS = True
    CORS_ORIGIN_ALLOW_ALL = True
    CORS_ORIGIN_WHITELIST = ()
    
    CORS_ALLOW_METHODS = (
        'DELETE',
        'GET',
        'OPTIONS',
        'PATCH',
        'POST',
        'PUT',
        'VIEW',
    )
    
    CORS_ALLOW_HEADERS = (
        'accept',
        'accept-encoding',
        'authorization',
        'content-type',
        'dnt',
        'origin',
        'user-agent',
        'x-csrftoken',
        'x-requested-with',
    )
    

你可能感兴趣的:(vue,rest-framework,django)