-
-
Notifications
You must be signed in to change notification settings - Fork 538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for decorators #422
Comments
Thanks. This requires quite a re-imagination of the way the library works and I'd like to engage in this but don't have time right now. What's your use case, do you have something you can't achieve with Thanks, Adam |
Coming from the flask world I feel the same. Working with a single regular expression to convey your rules is a bit clunky, I would much rather decorate an endpoint with specific options. This is the decorator I typically use with |
Another vote for this here: I run the Django admin on a domain that also hosts a couple of API endpoints. I want to allow CORS for those endpoints, and I was hoping I could do it with a view decorator rather than having to add a middleware that affects every request to my site. I'll use |
Another request here as well. We run a subdomain and need to adjust some of the cors headers in various ways on the per api basis. Unfortunately these are public apis so can't be moved about now :( |
I had to apply CORS to exactly one view and came up with this solution:
This is comparable to how Django does it for the CSRF decorators (https://github.com/django/django/blob/236e6cb5881168a79a194b43c2d8dff7a14c3f03/django/views/decorators/csrf.py#L1) By adding a init method to the middleware class that takes extra configuration (overrides of defaults/settings) would make this even more flexible (using |
Similarly to @jaap3's solution, but for function based views: from corsheaders.middleware import CorsMiddleware
from django.utils.decorators import decorator_from_middleware
from django.views.decorators.csrf import csrf_exempt
cors = decorator_from_middleware(middleware_class=CorsMiddleware)
@cors
@csrf_exempt
def some_function(request):
... It will read |
+1 for the decorators. I work on legacy code where the API is scattered across many different url's that don't have any sensible regex pattern. A view decorator would be very useful. |
https://docs.djangoproject.com/en/4.2/ref/utils/#django.utils.decorators.decorator_from_middleware Turns out that PR #852 converted the Sadly there seem to be no plans to make |
I was able to get class CustomCorsMiddleware(CorsMiddleware):
def process_request(self, request):
response = self.check_preflight(request)
if response is not None:
# Add the CORS headers to the preflight response
response = self.process_response(request, response)
return response
def process_response(self, request, response):
return self.add_response_headers(request, response) Use at your own risk. It basically re-implements the |
I've seen in the past people were talking about having support for decorators instead of being stuck with just
CORS_URLS_REGEX
I'd like to re-open the request to support decorators so that it's very easy to limit CORS responses to specific views.
The text was updated successfully, but these errors were encountered: