📜  django-cors-headers - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:30:30.742000             🧑  作者: Mango

Django-Cors-Headers - Shell-Bash

Introduction

Django-Cors-Headers is a Django application that adds CORS (Cross-Origin Resource Sharing) headers to responses in a Django application.

CORS is a mechanism that allows many resources (e.g., fonts, JavaScript, etc.) on a web page to be requested from another domain outside the domain from which the resource originated. CORS headers need to be added to the response from the server to allow cross-origin requests.

Installation

You can install Django-Cors-Headers using pip:

pip install django-cors-headers
Usage

To use Django-Cors-Headers, add 'corsheaders' to your INSTALLED_APPS and add CorsMiddleware to your middleware:

# settings.py

INSTALLED_APPS = [
    # ...
    'corsheaders',
    # ...
]

MIDDLEWARE = [
    # ...
    'corsheaders.middleware.CorsMiddleware',
    # ...
]

By default, Django-Cors-Headers allows all origins to make requests. You can change this behavior by adding the desired origin(s) to the CORS_ORIGIN_WHITELIST setting:

# settings.py

CORS_ORIGIN_WHITELIST = [
    'https://example.com',
    'https://www.example.com',
]
Conclusion

In conclusion, Django-Cors-Headers is a useful tool for allowing cross-origin requests in a Django application. By adding the required headers to responses from the server, it enables resources to be requested from other domains outside of the domain from which the resource originated.