473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Django CORS issue in Deployment

Facing CORS issue in after deployment
using Django Rest Framework using Swagger

After running API's in swagger I am getting this Error in deployment

Error is

**Failed to fetch.
Possible Reasons:
CORS
Network Failure
URL scheme must be "http" or "https" for CORS request.**

it was working fine in local

My settings.py




Expand|Select|Wrap|Line Numbers
  1. Settings.py
  2.  
  3. INSTALLED_APPS = [
  4.    " ADDED ALL APPS"
  5.     'corsheaders',
  6.     '
  7.  
  8. ]
  9.  
  10. MIDDLEWARE = [
  11.     'django.middleware.security.SecurityMiddleware',
  12.     'django.contrib.sessions.middleware.SessionMiddleware',
  13.     'corsheaders.middleware.CorsMiddleware',
  14.     'django.middleware.common.CommonMiddleware',
  15.     'django.middleware.csrf.CsrfViewMiddleware',
  16.     'django.contrib.auth.middleware.AuthenticationMiddleware',
  17.     'django.contrib.messages.middleware.MessageMiddleware',
  18.     'django.middleware.clickjacking.XFrameOptionsMiddleware',
  19.     'django.middleware.csrf.CsrfViewMiddleware',
  20.     'corsheaders.middleware.CorsPostCsrfMiddleware',
  21.     "django.middleware.security.SecurityMiddleware",
  22.     "whitenoise.middleware.WhiteNoiseMiddleware",
  23. ]
  24.  
  25. CORS_ALLOW_METHODS = [
  26.     'DELETE',
  27.     'GET',
  28.     'OPTIONS',
  29.     'PATCH',
  30.     'POST',
  31.     'PUT',
  32. ]
  33. CORS_ALLOW_HEADERS = [
  34.     "accept",
  35.     "accept-encoding",
  36.     "authorization",
  37.     "content-type",
  38.     "dnt",
  39.     "origin",
  40.     "user-agent",
  41.     "x-csrftoken",
  42.     "x-requested-with",
  43. ]
  44.  
  45. CORS_ALLOW_ALL_ORIGINS = True
  46.  
  47. CORS_ALLOWED_ORIGINS  = [
  48.    "HERE MY DOMAIN"
  49.     ]
  50.  
  51.  
  52.  
  53.  
  54.  
Feb 23 '23 #1
1 11578
Varsha1285
16 16bit
CORS (Cross-Origin Resource Sharing) issues occur when a web application hosted on one domain tries to access resources (APIs, fonts, images, etc.) on another domain. CORS is a security mechanism implemented by web browsers to prevent cross-origin requests unless explicitly allowed.

To resolve CORS issues in Django Rest Framework (DRF) after deployment and when using Swagger, you can follow these steps:

1-Install the django-cors-headers package by running the following command.
2-Add 'corsheaders' to the INSTALLED_APPS setting in your Django project's settings.py file
3-Define the CORS origin settings in your settings.py file. You can set it to allow all origins using the CORS_ORIGIN_ALLOW_ALL option, or specify specific origins using the CORS_ORIGIN_WHITELIST option
4-Restart your Django server to apply the changes.

By following these steps, you should be able to resolve CORS issues when using Django Rest Framework with Swagger. Make sure to adjust the CORS origin settings according to your specific deployment configuration and security requirements.
May 19 '23 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Ilias Lazaridis | last post by:
I have implemented a simple schema evolution support for django, due to a need for a personal project. Additionally, I've provided an Audit: http://case.lazaridis.com/wiki/DjangoAudit As a...
92
by: Ray | last post by:
I just moved to another company that's mainly a Java/.NET shop. I was happy to find out that there's a movement from the grassroot to try to convince the boss to use a dynamic language for our...
12
by: Dave U. Random | last post by:
http://snipr.com/PracticalDjango
4
by: K | last post by:
Hello everyone, I understand that urllib and urllib2 serve as really simple page request libraries. I was wondering if there is a library out there that can get the HTTP requests for a given...
1
by: Aspersieman | last post by:
On Wed, 05 Nov 2008 08:35:23 +0200, 3000 billg <billg3000@hos.twgg.org> wrote: Hi Excellent choice :)
1
by: imhomer | last post by:
I met this problem (Django base.py: 'str' object is not callable) when I go through a Django tutorial: photo/models.py: from django.db import models from django.contrib.auth.models import User...
1
by: samvb | last post by:
Hey Fellas, I am seriously in despair. I have a CI driven app in domaina.com. I need to send cross domain request to a SINGLE controller in it from domainb.com. It will be an ajax call using POST...
0
by: samvb | last post by:
Hi, We have a DRF backend API and two front-end applications: a CRUD and a report server. Currently, we have issue with CORS: These are the domains: sever.com:8098 == >backend and front...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.