Breaking

Code Gripper
Responsive Advertisement
  • Home
HomeDjango

How to enable CORS on Django REST Framework

byMahabubur Rahman -November 12, 2022
0

You can install python package first - 

python -m pip install django-cors-headers

and then add it to your installed apps:


INSTALLED_APPS = ( ... 'corsheaders', ... )

You will also need to add a middleware class to listen in on responses:

MIDDLEWARE = [ ..., 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ..., ]


CorsMiddleware should be placed as high as possible, especially before any middleware that can generate responses such as Django's CommonMiddleware or Whitenoise's WhiteNoiseMiddleware. If it is not before, it will not be able to add the CORS headers to these responses.


Also if you are using CORS_REPLACE_HTTPS_REFERER it should be placed before Django's CsrfViewMiddleware (see more below).

and specify domains for CORS, e.g.:



CORS_ALLOWED_ORIGINS = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000",
]

Previously this setting was called CORS_ORIGIN_WHITELIST, which still works as an alias, with the new name taking precedence.





Tags Django
  • Facebook
  • Twitter

You may like these posts

Post a Comment

Previous Post Next Post

Follow Us

Popular Posts

number

How to get only number from string in Python?

byMahabubur Rahman-September 10, 2021

ModuleNotFoundError: No module named 'psycopg2' django

September 17, 2021

How to change the date format in Laravel view page

September 11, 2021

How to get current day date in JavaScript

August 19, 2021

Subscribe Us

Facebook

Categories

Main Tags

Code Gripper

September 30, 2023

Total Pageviews

Search This Blog

CODE GRIPPER

Code Gripper
CODE GRIPPER

  • Home
  • Python
  • PHP

Menu Footer Widget

  • Home
  • About
  • Contact Us
Code Gripper

About Us

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's.

Categories

Main Tags

Latest Posts

Most Popular

How to get only number from string in Python?

September 10, 2021

ModuleNotFoundError: No module named 'psycopg2' django

September 17, 2021

How to change the date format in Laravel view page

September 11, 2021
Design by Blogger | Distributed by Gooyaabi
  • Home
  • About Us
  • Contact Us
  • RTL Version

Contact Form