#Allow any Hosts / origin on a specific view
13 messages · Page 1 of 1 (latest)
??
I believe without a middleware its not possibvle?
what exactly are you trying to do?
read my question sir
I did,
so: basically
I use the global settings for allowed hosts, allowed origins,.. from my global settings.py which is default django.
But I have one view/api-path I want to allow from anywhere.
you are using CorsHeaders ?
yes
inherit cors middleware, overwrite the method that checks if origin is allowed, add your condition for that particular route
I think that would work
pls give me any reference? @spark swan
# settings.py
CORS_OPTIONAL_ORIGINS = {
"https://foobar.com": ["path-to-view"]
}
# middleware.py
from django.conf import settings
from corsheaders.middleware import CorsMiddleware as DjangoCorsMiddleware
class CorsMiddleware(DjangoCorsMiddleware):
def _url_in_optional_whitelist(self, origin: str) -> bool:
return origin in settings.SCORS_OPTIONAL_ORIGINS
def origin_found_in_white_lists(self, origin: str, url: SplitResult) -> bool: # overwrite
if self._url_in_optional_whitelist(origin):
# check requested path is allowed route
return condition
return super().origin_found_in_white_list(origin, url)