简体   繁体   中英

How to include static files from outside the project root in django

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.

my_script.js works like this

<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!

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM