在Local -Host开发中,settings.py文件中的访问域(和端口)URL

发布于 2025-02-06 04:21:33 字数 1211 浏览 1 评论 0原文

您如何在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 or reverse 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲娇萝莉攻 2025-02-13 04:21:33

您无法访问settings.py中的域。当您运行./ manage.py runserver 0:8000时,您告诉Django在Localhost的端口8000上收听。但是您无法从这台机器中分辨出该请求将要出现到该机器上。例如,您可以将DNS配置为发送www.domain1.comwww.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,然后随时检查该变量。

如果您确实需要自动检测此此操作,则可以:

  1. 查询本地计算机的名称(机器名称,而不是域名,如上所述)
  2. 检查该名称是否匹配您的本地开发机器(因此您不在prod中执行此操作,等等)
  3. 检查您是否没有互联网(ping google.com之类的东西,看看它是否成功或时间出现),

我不建议这种方法;有很多边缘案例。另外,如果您想在“没有互联网的本地环境中”并手动设置环境变量,您可能可以自己确定自己。

使用CDN提供的缓存文件

,我认为最简单的方法(尽管有点详细)是在模板中放置语句,以定位语句来定位CDN或您的本地版本(请注意,您必须必须通过local_no_internet在视图的上下文中):

{% if LOCAL_NO_INTERNET %}
  <script src="{% static "cdncache/bootstrap.min.js" %}"></script>
{% else %}
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="..." crossorigin="anonymous"></script>
{% endif %}

或者,您可以在这里做一些更复杂的事情,例如中间件,以某种方式替换对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 send www.domain1.com and www.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 with scutil --get LocalHostName and on other Unixes you can cat /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:

  1. Query for the local machine's name (machine name, not domain, as described above)
  2. Check if the name matches your local dev machine (so that you don't do this thing in prod, etc.)
  3. Check if you don't have internet (ping google.com or something and see if it comes back successfully or times out)

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 pass LOCAL_NO_INTERNET in the context of the view):

{% if LOCAL_NO_INTERNET %}
  <script src="{% static "cdncache/bootstrap.min.js" %}"></script>
{% else %}
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="..." crossorigin="anonymous"></script>
{% endif %}

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.

梦中楼上月下 2025-02-13 04:21:33

我从未找到过找到有关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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文