如何从Django加载模型管理文件中的静态文件?

时间:2023-02-05 13:09:52

I have my Django static files in Amazon Servers, and I trying to load these files in my Model Admin File, but its only work with absolute URLs.

我在Amazon Servers中有我的Django静态文件,我试图在我的模型管理文件中加载这些文件,但它只能使用绝对URL。

In my Django templates I load and call my static files with

在我的Django模板中,我加载并调用我的静态文件

{% load admin_static %}
<script type="text/javascript" src="{% static "js/myfile.js" %}"></script>

in settings.py i set this

在settings.py我设置了这个

STATIC_URL = 'https://s3.mydomain.com/static/'

and in my Model Admin, in moment, it only works with

在我的模型管理员中,在一瞬间,它只适用于

class Media:
    js = ("https://s3.mydomain.com/static/myfile.js",
          "https://s3.mydomain.com/static/myfile2.js",)

How can I load these files only with the static file name? I trying

如何仅使用静态文件名加载这些文件?我在尝试

class Media:
    js = ("{% static "js/myfile.js" %}",
          "{% static "js/myfile2.js" %}",)

but doesn't work.

但不起作用。

1 个解决方案

#1


2  

How about this:

这个怎么样:

In your settings file:

在您的设置文件中:

STATIC_URL = "https://aws.domain.com/"

It is standard settings. Note, that it must end in a slash if set to a non-empty value.

这是标准设置。请注意,如果设置为非空值,则必须以斜杠结尾。

from django.conf import settings

class Media:
    js = (settings.STATIC_URL + "js/myfile.js",
          settings.STATIC_URL + "js/myfile2.js")

#1


2  

How about this:

这个怎么样:

In your settings file:

在您的设置文件中:

STATIC_URL = "https://aws.domain.com/"

It is standard settings. Note, that it must end in a slash if set to a non-empty value.

这是标准设置。请注意,如果设置为非空值,则必须以斜杠结尾。

from django.conf import settings

class Media:
    js = (settings.STATIC_URL + "js/myfile.js",
          settings.STATIC_URL + "js/myfile2.js")