Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add my own files to django 'static' folder

Tags:

python

django

I've read django static files document and made my django static files settings like this

setting.py

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'

html page

<img src="{% static "admin/img/author.jpg" %}" alt="My image"/>

So if I address one of django default static files, it works fine. But if I add my own file and folders to the static folder, it doesn't show it.

I tried

python manage.py collectstatic

But nothing changed. How can I make it work?

like image 928
Alex Jolig Avatar asked Nov 23 '25 07:11

Alex Jolig


1 Answers

A few things...

STATICFILES_DIRS = (
    'path/to/files/in/development',
)

STATIC_ROOT = 'path/where/static/files/are/collected/in/production'

When DEBUG = True, Django will automatically serve files located in any directories within STATICFILES_DIRS when you use the {% static 'path/to/file' %} template tag.

When DEBUG = False, Django will not serve any files automatically, and you are expected to serve those files using Apache, Nginx, etc, from the location specified in STATIC_ROOT.

When you run $ manage.py collectstatic, Django will copy any and all files located in STATICFILES_DIRS and also files within any directory named 'static' in 3rd party apps, into the location specified by STATIC_ROOT.

I typically structure my project root as such:

my_project/
    /static_assets/  (specified in STATICFILES_DIRS)
        /js
        /images

    /static (specified in STATIC_ROOT)

I hope that helps you understand how the staticfiles app works.

like image 147
Brandon Avatar answered Nov 25 '25 20:11

Brandon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!