[Answer]-Class based views, response headers and CORS

1πŸ‘

βœ…

To make this work, you need to allow the external web service to make cross-origin request. The easiest way to do it is by installing django-cors-headers package and add your service hostname in the settings.py like so:

CORS_ORIGIN_WHITELIST = (
    'myservice.com',
)

where myservice.com is the external service that makes the ajax call. Also remember to follow these setup instructions.

πŸ‘€mariodev

0πŸ‘

For Django Framework itself, you can do the following:

Instead of URL β€˜https://origin.comβ€˜ you can place a wildcard β€˜*’ or any other URL.

class PriceListTableView(TemplateView):
    template_name = "pages/price_list_table.html"

    def get(self, request, *args, **kwargs):
        response = super().get(request, *args, **kwargs)
        response['Access-Control-Allow-Origin'] = 'https://origin.com'
        return response

Might or might not apply to Django REST Framework.

πŸ‘€Maziyar Mk

Leave a comment