基于 IP 或主机名的 Django 设置
我想在我的设置中添加一些内容,例如
if ip in DEV_IPS:
SOMESETTING = 'foo'
else:
SOMESETTING = 'bar'
是否有一种简单的方法来获取 IP 或主机名 - 还有 - 这是一个坏主意吗?
I'd like to have something in my settings like
if ip in DEV_IPS:
SOMESETTING = 'foo'
else:
SOMESETTING = 'bar'
Is there an easy way to get the ip or hostname - also - is this is a bad idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但是,我建议不要这样做,而是为您正在使用的每个环境维护多个设置文件。
__init__.py
具有所有默认值。在 qa.py 和任何其他设置文件的顶部,第一行包含:后跟该特定环境所需的任何覆盖。
However, I'd recommend against this and instead maintain multiple settings file for each environment you're working with.
__init__.py
has all of your defaults. At the top ofqa.py
, and any other settings file, the first line has:followed by any overrides needed for that particular environment.
一些商店使用的一种方法是在每台计算机上设置环境变量。或许可以称之为“环境”。在 POSIX 系统中,您可以在用户的 .profile 文件中执行类似
ENVIRONMENT=product
的操作(对于每个 shell 和操作系统,这会略有不同)。然后在settings.py
中你可以执行如下操作:One method some shops use is to have an environment variable set on each machine. Maybe called "environment". In POSIX systems you can do something like
ENVIRONMENT=production
in the user's .profile file (this will be slightly different for each shell and OS). Then insettings.py
you can do something like this: