jalanga
jalanga

Reputation: 1576

It is possbile to have ALLOWED_HOSTS different configuration for some urls?

I want to have ALLOWED_HOSTS=['*'] for some urls but for the rest I want it to be ALLOWED_HOSTS=[".example.com"].

For csrf we have @csrf_exempt

For cors we have the signal check_request_enabled

But for ALLOWED_HOSTS?

Upvotes: 0

Views: 203

Answers (1)

May.D
May.D

Reputation: 1910

A way to go would be to write a middleware, that will check the request url and set ALLOWED HOSTS. You'll need to add this middleware at the top of MIDDLEWARES section in settings file. Try something like below:

from django.conf import settings

def simple_middleware(get_response):
    def middleware(request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.
        if request.META['PATH_INFO'] == "your_logic":
            settings.ALLOWED_HOSTS = ["*"]
        else:
            settings.ALLOWED_HOSTS = ['example.com']
        response = get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response

    return middleware

Upvotes: 1

Related Questions