When we develop rest api and want to consume form different website we have to enable cors.
In this tutorial i will show you how to enable cors
Install package
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 in seetings.py:
MIDDLEWARE_CLASSES = (
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
)
Now i will give two alternative anyone you can do
ALLOWED_HOSTS=['*']
CORS_ORIGIN_ALLOW_ALL = True
second alternative
ALLOWED_HOSTS=['http://localhost:5000']
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
'http://localhost:5000',
)
Thank you for reading my article! If you enjoyed it and would like to support my work, please consider buying me a coffee at Buy Me a Coffee. You can also learn more about me and my work by visiting my Giasuddin Bio and following me on LinkedIn and Twitter. Thank you for your support!
Top comments (0)