16

Firstly, I want to say that I'm a beginner in Django.

I'm looking for a simple way to retrieve the domain name of my Django website.

I want to do this in my settings.py. I've already tried with the socket something like this:

socket.gethostname()

but this doesn't work correctly.

orde
  • 5,233
  • 6
  • 31
  • 33
Maître Van Diest
  • 215
  • 1
  • 3
  • 11

3 Answers3

16

If you have a request object,do

request.META['HTTP_HOST']

This would return the hostname

Imprfectluck
  • 654
  • 6
  • 26
  • 12
    Probably better to use `request.get_host()` ([docs link](https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.get_host)), since HTTP_HOST is not the only way to find the host name. – S. Kirby Apr 19 '16 at 19:18
9

If you're using django.contrib.sites framework:

from django.contrib.sites.models import Site

your_domain = Site.objects.get_current().domain

Reference: https://docs.djangoproject.com/en/1.8/ref/contrib/sites/

Amaury Medeiros
  • 2,093
  • 4
  • 26
  • 42
0
import platform
platform.node()

from the docs:

"Returns the computer’s network name (may not be fully qualified!). An empty string is returned if the value cannot be determined."

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143