How can I enable CORS on Django REST Framework

0 votes
How can I enable CORS on my Django REST Framework?
Jun 30, 2020 in Python by kartik
• 37,520 points
4,211 views

1 answer to this question.

0 votes

Hello @kartik,

Take the following steps to enable CORS for Rest API hosted in Django application. I assume you already have REST API developed and hosted. I just focus on how to allow resources to be accessed from cross domain.

  • Install django-cors-headers using PIP as follows:
      pip install django-cors-headers
  • Add corsheaders to installed applications in settings file:
      INSTALLED_APPS = [ ... 'corsheaders', ]
    • Add corsheaders.middleware.CorsMiddleware to middleware section in settings file:
          MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',    'django.middleware.common.CommonMiddleware', ... ]
      • Allow access to all domains by setting the following variable to TRUE in settings file:
            CORS_ORIGIN_ALLOW_ALL = True
        • Alternatively, you can specify which domains must be given access by creating following variables in settings file:
             CORS_ORIGIN_ALLOW_ALL = False CORS_ORIGIN_WHITELIST = ( 'http://localhost:4200', )

          That's all you have to do to access resources of django application from other applications hosted on other servers.

          Hope it helps!

          Thanks!

          answered Jun 30, 2020 by Niroj
          • 82,880 points

          Related Questions In Python

          0 votes
          1 answer

          How can modify request.data in django REST framework?

          Hello @kartik, Generally request in drf views is rest_framework.request.Request instance. Following to ...READ MORE

          answered Jul 2, 2020 in Python by Niroj
          • 82,880 points
          5,305 views
          0 votes
          1 answer

          How can I enable project_id parameter of DJango to be optional?

          Hello @kartik, There are several approaches. One is to ...READ MORE

          answered Jul 3, 2020 in Python by Niroj
          • 82,880 points
          879 views
          0 votes
          1 answer

          how to download and install Django rest framework?

          To install Django, you can simply open ...READ MORE

          answered Apr 24, 2018 in Python by Christine
          • 15,790 points
          1,854 views
          0 votes
          1 answer

          How to temporarily disable a foreign key constraint in MySQL?

          Hello @kartik, To turn off foreign key constraint ...READ MORE

          answered Jun 23, 2020 in Python by Niroj
          • 82,880 points
          2,321 views
          0 votes
          1 answer

          How do I use Django templates without the rest of Django?

          Hello @kartik, Let's say you have this important ...READ MORE

          answered Jun 23, 2020 in Python by Niroj
          • 82,880 points
          1,477 views
          0 votes
          1 answer

          How can I enable CORS on Django REST Framework

          Hello @kartik, You can do by using a ...READ MORE

          answered Jul 1, 2020 in Python by Niroj
          • 82,880 points
          1,938 views
          0 votes
          1 answer

          How do I include related model fields using Django Rest Framework?

          Hello @kartik, The simplest way is to use the ...READ MORE

          answered Jul 1, 2020 in Python by Niroj
          • 82,880 points
          3,548 views
          webinar REGISTER FOR FREE WEBINAR X
          REGISTER NOW
          webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP