如何在子域或主域上签入 Django

How do I check in Django when on a subdomain or the main domain

如何在 settings.py 中创建 if 语句,如下例所示,以检查站点是在子域还是主域中。

if main_domain == request.get_host():
    HOST_THEMES = [(main_domain, 'maindomain_theme'),
if subdomain == request.get_host():
    HOST_THEMES = [(subdomain, 'subdomain_theme'),

目前只有显式设置子域的选项,当 运行 多个子域时它不起作用。一定有更好的方法来做到这一点。

在settings.py中,可以使用socket.gethostname()

import socket

if 'subdomain' in socket.gethostname():
    # set your subdomain
else:
    # set your maindomain

另一个版本:

if socket.gethostname().split('.')[0] in ['subdomain1', 'subdomain2', ..]:
    # set socket.gethostname().split('.')[0] as subdomain