69

How to use Django to get the name for the host server?

I need the name of the hosting server instead of the client name?

Ron
  • 22,128
  • 31
  • 108
  • 206
user469652
  • 48,855
  • 59
  • 128
  • 165

9 Answers9

111

I generally put something like this in settings.py:

import socket

try:
    HOSTNAME = socket.gethostname()
except:
    HOSTNAME = 'localhost'
Craig Trader
  • 15,507
  • 6
  • 37
  • 55
85

If you have a request (e.g., this is inside a view), you can look at request.get_host() which gets you a complete locname (host and port), taking into account reverse proxy headers if any. If you don't have a request, you should configure the hostname somewhere in your settings. Just looking at the system hostname can be ambiguous in a lot of cases, virtual hosts being the most common.

the
  • 21,007
  • 11
  • 68
  • 101
Tobu
  • 24,771
  • 4
  • 91
  • 98
  • In case i don't have a request, I need to hard-code domain name in settings variable? – Yukulélé Nov 26 '18 at 09:48
  • for me `request.get_raw_uri()` did the job, as it also provided the protocol http(s). This is in Django 3 though, not sure when this was introduced. – Donald Mar 29 '20 at 19:56
8

Just add to @Tobu's answer. If you have a request object, and you would like to know the protocol (i.e. http / https), you can use request.scheme (as suggested by @RyneEverett's comment).

Alternatively, you can do (original answer below):

if request.is_secure():
    protocol = 'https'
else:
    protocol = 'http'

Because is_secure() returns True if request was made with HTTPS.

azalea
  • 11,402
  • 3
  • 35
  • 46
8

If you need to get http(s)://hostname/ you can use the following:

request.build_absolute_uri('/')

All useful methods are listed here

Sofien
  • 1,302
  • 11
  • 21
6

Try os.environ.get('HOSTNAME')

coder
  • 8,346
  • 16
  • 39
  • 53
Ankit Jaiswal
  • 22,859
  • 5
  • 41
  • 64
  • When using this, be aware that some distro's only set HOST, while others only set HOSTNAME, and that many *export* neither by default. – BrtH Apr 21 '21 at 23:59
4

Basically, You can take with request.get_host() in your view/viewset. It returns <ip:port>

direwolf
  • 73
  • 5
4

If you have a request object, you can use this function:

def get_current_host(self, request: Request) -> str:
    scheme = request.is_secure() and "https" or "http"
    return f'{scheme}://{request.get_host()}/'
Tobias Ernst
  • 4,214
  • 1
  • 32
  • 30
0
request.get_raw_uri() # example https://192.168.32.181:10555/
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 3
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Suraj Rao Sep 30 '21 at 08:32
-1

To get my django server name I tried this

host = f"{ request.scheme }://{ request.META.get('REMOTE_ADDR') }"
sultanmyrza
  • 4,551
  • 1
  • 30
  • 24
  • 1
    `REMOTE_ADDR` is usually the IP address of the client. The only time that is going to be the host server is if the request is made from localhost. – Stephen Ostermiller Nov 01 '21 at 07:35