gkpo
gkpo

Reputation: 2662

Django pipeline - How to serve static files after running collectstatic?

My project structure

|- myProject
    |- manage.py
    |- myProject
        |- settings.py
        |- static // This is my STATIC_ROOT
    |- myApp
        |- static // This is where I edit and save my static files

So I run the collectstatic command. My css/js files are gathered (and compressed into a single file) into the STATIC_ROOT directory (myProject/myProject/static). So far so good.

First question: How do I serve them?

I tried calling http://127.0.0.1:8000/static/css/styles.css but it returns a 404 file not found error.

I tried adding STATIC_ROOT in the STATICFILES_DIRS but then I get the error:

The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.

Second question: Can Django serve "project-wide" static files?

I know that you can serve "application-wide" static files by calling "/static/myApp/my-static-file.css". But does Django know about myProject/myProject/static the way it knows about the static folder inside each app? Or is it something you need to tell explicitly?

I've read some blog/forum posts saying that you need to change urls.py to but I tried, without success. Do I have to?

I find it pretty hard to find information about static files in Django's documentation and I'm very confused right now.


EDIT: now I am really confused: I tried setting PIPELINE_ENABLED = False and now this url works. http://127.0.0.1:8000/static/css/styles.css but it doesn't help because when PIPElINE_ENABLED is False, it doesn't try to call the compressed css, but all files separately.

Upvotes: 1

Views: 1303

Answers (1)

Joshua Olson
Joshua Olson

Reputation: 3803

I needed to set DEBUG = True and PRODUCTION = False to get it to serve static files. Just the DEBUG = True would not do it.

Upvotes: 0

Related Questions