如何在django中包含项目根目录外部的静态文件

时间:2023-02-05 12:46:38

My structure looks likte this:

我的结构看起来像这样:

  conf/
       my_other_script.js         <-- The file I want to include
    project/
      appname/
        static/ 
          appname/  
            my_script.js <-- current script that I include
        views.py
        ...

  templates/    
    base.html

  settings.py
  views.py

How do I include my_other_script.js in the HTML which is in the conf folder on the same level as the project folder.

如何在与项目文件夹相同级别的conf文件夹中的HTML中包含my_other_script.js。

my_script.js works like this

my_script.js就是这样的

<script type='text/javascript' src="{% static 'appname/my_script.js' %}"></script>

I tried with ../ but didnt't work. Also, couldn't find anything in the docs anywhere.

我试过../但是没有用。此外,在任何地方的文档中都找不到任何内容。

Anyone an idea? Thanks in advance!

有人有想法吗?提前致谢!

1 个解决方案

#1


1  

Add the absolute path to that directory in STATICFILES_DIRS setting. You can calculate from the settings.py dir, just add a variable like this:

在STATICFILES_DIRS设置中添加该目录的绝对路径。您可以从settings.py目录计算,只需添加如下变量:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

And then add the static files path, ie:

然后添加静态文件路径,即:

STATICFILES_DIRS = [os.path.abspath(os.path.join(
        BASE_DIR, '..', 'conf', 'project', 'appname', 'static'))]

In production, collectstatic will pick it up.

在生产中,collectstatic将接收它。

For more info check out Django's howto manage static files.

有关更多信息,请查看Django如何管理静态文件。

#1


1  

Add the absolute path to that directory in STATICFILES_DIRS setting. You can calculate from the settings.py dir, just add a variable like this:

在STATICFILES_DIRS设置中添加该目录的绝对路径。您可以从settings.py目录计算,只需添加如下变量:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

And then add the static files path, ie:

然后添加静态文件路径,即:

STATICFILES_DIRS = [os.path.abspath(os.path.join(
        BASE_DIR, '..', 'conf', 'project', 'appname', 'static'))]

In production, collectstatic will pick it up.

在生产中,collectstatic将接收它。

For more info check out Django's howto manage static files.

有关更多信息,请查看Django如何管理静态文件。