Tom
Tom

Reputation: 2661

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!

Upvotes: 0

Views: 2467

Answers (1)

jpic
jpic

Reputation: 33410

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.

Upvotes: 1

Related Questions