Как получить хост запроса в django?

Как получить хост запроса в django? как мне получить URL/имя хоста из запроса

?
class StoreLocations(APIView):
    """
    GET     :  For fetching store locations
    Returns :  Store Locations
    ---------------
    json  
    """
    permission_classes = []
    http_method_names = ["get"]

    def get(self, request, format=None):
            """
            Want to check from where request is coming from and allow requests coming from specific hosts only
            """

Вы можете использовать следующий фрагмент кода, чтобы увидеть, какой хост используется.

print(request.environ.get("HTTP_ORIGIN"))

Для просмотра дополнительных деталей, присутствующих в запросе.

print(request.__dict__)

запрос django имеет META словарь:https://docs.djangoproject.com/en/4.0/ref/request-response/#django.http.HttpRequest.META

Я думаю, что вы ищете:

request.META['REMOTE_ADDR'] # e.g. '1.1.1.1'
request.META['REMOTE_HOST'] # e.g. '1.1.1.1'
request.META['HTTP_REFERER'] # this show url from where was made request

Используйте HttpRequest.get_host(...)

class StoreLocations(APIView):
    def get(self, request, *args, **kwargs):
        host = request.get_host()
print(request.META.get("HTTP_ORIGIN"))
Вернуться на верх