Home > Net >  How to get request host in django?
How to get request host in django?

Time:07-28

How to get request host in django ? how will I get host URL/name from request

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 urls only
            """

CodePudding user response:

You can use the below code snippet to see which host is used.

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

To see the additional details present in request.

print(request.__dict__)

CodePudding user response:

django request have META dictionary:https://docs.djangoproject.com/en/4.0/ref/request-response/#django.http.HttpRequest.META

I think you search for:

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

CodePudding user response:

Use HttpRequest.get_host(...)

class StoreLocations(APIView):
    def get(self, request, *args, **kwargs):
        host = request.get_host()

CodePudding user response:

print(request.META.get("HTTP_ORIGIN"))
  • Related