Django - How to simply get domain name? [duplicate

2019-04-19 13:06发布

This question already has an answer here:

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.

3条回答
家丑人穷心不美
2楼-- · 2019-04-19 13:32
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."

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-19 13:42

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/

查看更多
狗以群分
4楼-- · 2019-04-19 13:51

If you have a request object,do

request.META['HTTP_HOST']

This would return the hostname

查看更多
登录 后发表回答