Skip to content

Commit

Permalink
fix: solving the CORS issues (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
yokwejuste authored Sep 21, 2024
1 parent 9a16c89 commit d364612
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/events/views/reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def get_serializer_class(self):
return ReservationSerializer
if self.action == "create":
return CreateReservationSerializer
return ReservationSerializer

def get_permissions(self):
"""
Expand Down
33 changes: 33 additions & 0 deletions website_api/settings/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,36 @@
"apps.users",
"apps.events",
]

# ---------------------- some extra stuff ------------------------------------- #

EXTRA_MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"crequest.middleware.CrequestMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
]

# CORS configuration
CORS_ALLOW_ALL_ORIGINS = True

CORS_ALLOW_CREDENTIALS = True

CORS_ALLOWED_ORIGIN_REGEXES = [
r"^http://localhost$",
r"^https://\w+\.djangocameroon\.site",
r"^https://\w+\.djangocameroon\.site:$",
r"^https://\w+\.djangocameroon\.site:\d+$",
r"^https://\w+\.djangocameroon\.site:\d+/$",
]

CORS_ALLOWED_ORIGINS = [
"http://localhost",
"https://www.djangocameroon.site",
"https://djangocameroon.site",
]

CORS_ORIGIN_WHITELIST = [
"http://localhost",
"https://www.djangocameroon.site",
"https://djangocameroon.site",
]
9 changes: 4 additions & 5 deletions website_api/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dotenv import load_dotenv

from .apps import CUSTOM_APPS, THIRD_PARTY_APPS
from .apps import CUSTOM_APPS, THIRD_PARTY_APPS, EXTRA_MIDDLEWARE

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

Expand Down Expand Up @@ -30,19 +30,18 @@
INSTALLED_APPS += THIRD_PARTY_APPS
INSTALLED_APPS += CUSTOM_APPS

MIDDLEWARE = [
BASE_MIDDLEWARE = [
"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",

"crequest.middleware.CrequestMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
]

MIDDLEWARE = BASE_MIDDLEWARE + EXTRA_MIDDLEWARE

ROOT_URLCONF = "website_api.routes"

TEMPLATES = [
Expand Down
2 changes: 1 addition & 1 deletion website_api/settings/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@
}
INTERNAL_IPS = [
'127.0.0.1',
]
]

0 comments on commit d364612

Please sign in to comment.