Skip to content

Instantly share code, notes, and snippets.

@dojutsu-user
Last active December 9, 2018 07:42
Show Gist options
  • Save dojutsu-user/f65e73baab4d537d4c2d151960a4945f to your computer and use it in GitHub Desktop.
Save dojutsu-user/f65e73baab4d537d4c2d151960a4945f to your computer and use it in GitHub Desktop.
Setting up Django (with DRF) project to work with React
  • Install django-cors-headers: pipenv install django-cors-headers

  • In your settings.py

    • Add corsheaders to your INSTALLED_APPS
    • Add the following Middlewares:
    MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware', # new
    'django.middleware.common.CommonMiddleware', # new
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]
    • Add
    CORS_ORIGIN_WHITELIST = (
      'localhost:3000/'
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment