Django CORS issue in Deployment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sabarinathK
    New Member
    • Feb 2023
    • 1

    Django CORS issue in Deployment

    Facing CORS issue in after deployment
    using Django Rest Framework using Swagger

    After running API's in swagger I am getting this Error in deployment

    Error is

    **Failed to fetch.
    Possible Reasons:
    CORS
    Network Failure
    URL scheme must be "http" or "https" for CORS request.**

    it was working fine in local

    My settings.py




    Code:
    Settings.py
    
    INSTALLED_APPS = [
       " ADDED ALL APPS"
        'corsheaders',
        '
    
    ]
    
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'corsheaders.middleware.CorsPostCsrfMiddleware',
        "django.middleware.security.SecurityMiddleware",
        "whitenoise.middleware.WhiteNoiseMiddleware",
    ]
    
    CORS_ALLOW_METHODS = [
        'DELETE',
        'GET',
        'OPTIONS',
        'PATCH',
        'POST',
        'PUT',
    ]
    CORS_ALLOW_HEADERS = [
        "accept",
        "accept-encoding",
        "authorization",
        "content-type",
        "dnt",
        "origin",
        "user-agent",
        "x-csrftoken",
        "x-requested-with",
    ]
    
    CORS_ALLOW_ALL_ORIGINS = True
    
    CORS_ALLOWED_ORIGINS  = [
       "HERE MY DOMAIN"
        ]
  • Varsha1285
    New Member
    • Feb 2023
    • 16

    #2
    CORS (Cross-Origin Resource Sharing) issues occur when a web application hosted on one domain tries to access resources (APIs, fonts, images, etc.) on another domain. CORS is a security mechanism implemented by web browsers to prevent cross-origin requests unless explicitly allowed.

    To resolve CORS issues in Django Rest Framework (DRF) after deployment and when using Swagger, you can follow these steps:

    1-Install the django-cors-headers package by running the following command.
    2-Add 'corsheaders' to the INSTALLED_APPS setting in your Django project's settings.py file
    3-Define the CORS origin settings in your settings.py file. You can set it to allow all origins using the CORS_ORIGIN_ALL OW_ALL option, or specify specific origins using the CORS_ORIGIN_WHI TELIST option
    4-Restart your Django server to apply the changes.

    By following these steps, you should be able to resolve CORS issues when using Django Rest Framework with Swagger. Make sure to adjust the CORS origin settings according to your specific deployment configuration and security requirements.

    Comment

    Working...