{"library":"django-cors-headers","code":"# settings.py\n\nINSTALLED_APPS = [\n    # ... other apps\n    'corsheaders',\n    # ...\n]\n\nMIDDLEWARE = [\n    'corsheaders.middleware.CorsMiddleware',\n    'django.middleware.security.SecurityMiddleware',\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    'django.middleware.common.CommonMiddleware',\n    'django.middleware.csrf.CsrfViewMiddleware',\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    'django.contrib.messages.middleware.MessageMiddleware',\n    'django.middleware.clickjacking.XFrameOptionsMiddleware',\n    # ... other middleware\n]\n\n# Whitelist specific origins. In production, avoid CORS_ALLOW_ALL_ORIGINS = True.\nCORS_ALLOWED_ORIGINS = [\n    \"http://localhost:3000\",\n    \"http://127.0.0.1:3000\",\n    # Add your frontend domains here, e.g., \"https://yourfrontend.com\"\n]\n\n# Optional: Allow credentials (cookies, auth headers) to be sent cross-origin\n# CORS_ALLOW_CREDENTIALS = True\n\n# Optional: If you need to allow all origins for development (use with caution in production!)\n# CORS_ALLOW_ALL_ORIGINS = False # Set to True for development, but remove for production\n\n# Example for allowing specific HTTP methods if you deviate from default allowed methods\n# CORS_ALLOW_METHODS = [\n#     'DELETE',\n#     'GET',\n#     'OPTIONS',\n#     'PATCH',\n#     'POST',\n#     'PUT',\n# ]","lang":"python","description":"To quickly enable CORS, install the package, then add `corsheaders` to your `INSTALLED_APPS` and `CorsMiddleware` to the top of your `MIDDLEWARE` list in `settings.py`. Finally, configure `CORS_ALLOWED_ORIGINS` with a list of allowed frontend domains. For development, `CORS_ALLOW_ALL_ORIGINS = True` can be used temporarily but is not recommended for production due to security risks. Remember that middleware order is crucial for correct functionality.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}