在Local -Host开发中,settings.py文件中的访问域(和端口)URL
您如何在settings.py
django的文件中动态访问域名URL? (即“ http:// localhost:8000”)
我正在尝试在开发过程中互联网不可用时覆盖包装cdn,并希望指向static
文件目录中的本地文件。而os.path.join(base_dir,“路径/to/local.file”)应该起作用,而app/url(即” http:// localhost:8000/app/static/css/css/css/coms/url是上下文依赖的。 Bootstrap.min.css ”),而不仅仅是带有静态文件位置的主要域,并用./ manage.py runserver 0:8000
(即“ http:// localhost:8000/static/css) /bootstrap.min.css”)。
注意:
- 因为这在
settings.py
中,我无法加载任何应用程序或recters> reverse
,因为错误*** django.core.exceptions.AppRegistryNotReady:尚未加载应用程序。
- 我不在模板中,所以我无法使用
static
url - 静态定义它赢了'' 允许不同的端口加载
- t通过
./ manage.py runserver 0:8000
settings.py
基本上是python模块, 。基本上
是settings.py
# If in local dev
if "RDS_DB_NAME" not in os.environ:
# the setting for the package I am pointing to a local version
BOOTSTRAP5 = {
"css_url": {
### dynamically get domain here ###
# "href": os.path.join(LOCAL_DIR, "static/css/bootstrap.min.css"),
"href": "static/css/bootstrap.min.css",
}
How do you dynamically access the domain name URL in the settings.py
file of Django? (ie "http://localhost:8000")
I am trying to overwrite a package CDN while the internet is unavailable during development, and want to point to the local file in the static
files directory. While os.path.join(BASE_DIR, "path/to/local.file") should work, it is context-dependent as to which app/url (ie "http://localhost:8000/app/static/css/bootstrap.min.css
"), and not just the main domain with the static file location appended to the starting server with ./manage.py runserver 0:8000
(ie " http://localhost:8000/static/css/bootstrap.min.css").
Notes:
- Because this is in the
settings.py
, I can't load any apps orreverse
because of the error*** django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
- I am not in a template, so I can't use the
static
url - statically defining it won't allow for different port loadings when starting via
./manage.py runserver 0:8000
settings.py
is basically a python module, but how can you get the domain within it?
Basically in the settings.py
file:
# If in local dev
if "RDS_DB_NAME" not in os.environ:
# the setting for the package I am pointing to a local version
BOOTSTRAP5 = {
"css_url": {
### dynamically get domain here ###
# "href": os.path.join(LOCAL_DIR, "static/css/bootstrap.min.css"),
"href": "static/css/bootstrap.min.css",
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法访问
settings.py
中的域。当您运行./ manage.py runserver 0:8000
时,您告诉Django在Localhost的端口8000上收听。但是您无法从这台机器中分辨出该请求将要出现到该机器上。例如,您可以将DNS配置为发送www.domain1.com
和www.domain2.com
来到这台计算机上,从您的角度来看,它看起来像Localhost。因此,在请求提出之前,您无法知道“域”。但是,您的计算机确实具有本地计算机名称,您可以从
settings.py
中找到它。不同的OS会以不同的方式执行此操作,但是在Mac上,您可以使用scutil-get localhostname
以及在其他Unixes上获得它,您可以CAT/ETC/HOSTNAME
。克里斯·库维(Chris Curvey)是正确的,与环境之间的区分不同的方式是不同的设置文件。大多数开发人员都区分本地和生产环境,但是您提出了第三个:本地:没有互联网访问。
有2个步骤可以进行这项工作。
检测何时使用“没有互联网的本地”环境。
Du Jour Way(也是最简单的方法,IMO)可以使用环境变量。有很多方法可以做到这一点。例如,您可以在环境中设置
local_no_internet = true
,然后随时检查该变量。如果您确实需要自动检测此此操作,则可以:
我不建议这种方法;有很多边缘案例。另外,如果您想在“没有互联网的本地环境中”并手动设置环境变量,您可能可以自己确定自己。
使用CDN提供的缓存文件
,我认为最简单的方法(尽管有点详细)是在模板中放置语句,以定位语句来定位CDN或您的本地版本(请注意,您必须必须通过
local_no_internet
在视图的上下文中):或者,您可以在这里做一些更复杂的事情,例如中间件,以某种方式替换对CDN的引用。
我建议,而不是定义这个新的“没有Internet的本地”环境,您可以更改本地设置,以始终假设“没有互联网的本地”。 CDN应加快生产中的请求,但与始终使用自己的文件版本相比,在本地开发中并不是什么好。
You can't access the domain in
settings.py
. When you run./manage.py runserver 0:8000
you are telling Django to listen on port 8000 of whatever localhost is. But you can't tell from this machine which requests are going to come to this machine. For instance, you could have DNS configured to sendwww.domain1.com
andwww.domain2.com
to come to this machine which looks like localhost from your perspective. So you can't know "domain" until a requests comes in.Your machine does however have a local machine name which you could figure out from
settings.py
. Different OSes do this differently but on Macs you can get it withscutil --get LocalHostName
and on other Unixes you cancat /etc/hostname
.Chris Curvey is right that the canonical way to differentiate between environments is with different settings files. Most devs differentiate between local and production environments but you are proposing a third: local with no internet access.
There are 2 steps to making this work.
Detect when to use the "local with no internet" environment.
The du jour way (and easiest way, IMO) to do this is with an environment variable. There are lots of ways to do this. You could, for instance, set
LOCAL_NO_INTERNET=True
in the environment and then check that variable anytime you need to do something special in your code.If you really need to detect this automatically you could:
I do not recommend this approach; there are many edge cases. Plus you probably can determine for yourself if you would like to be in "local with no internet" environment and set the environment variable manually.
Serve cached files usually provided by CDN
I think the easiest way, although a little verbose, is to put an
if
statement in your templates to either target the CDN or your local version (note that you will have to passLOCAL_NO_INTERNET
in the context of the view):Or you could do something way more complicated here like a middleware to somehow replace references to CDNs.
I would suggest that instead of defining this new "local with no internet" environment you could change your local setup to always assume "local with no internet". The CDN should speed up requests in production but doesn't do you much good in local development compared with always using your own version of those files.
我从未找到过找到有关Django听众的详细信息的方法。
在不同环境中拥有不同设置的规范方法是具有不同的设置文件,然后设置DJANGO_SETTINGS_MODULE环境变量以控制使用哪个。
I've never found a way to find out details about the Django listener.
The canonical way to have different settings in different environments is to have different settings files, and then set the DJANGO_SETTINGS_MODULE environment variable to control which one is used.