1
votes

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.

1
How are you serving django (e.g. via runserver which would be my guess based on the URL you're using, versus via apache/nginx/etc.?) And are you running in debug mode?Foon
Yes I am running django with runserver. And yes I have DEBUG = Truegkpo
If you are running the django server, it will serve from the static_root (given everything is properly configured). collectstatic is used to get all static files in a place the reverse proxy can find them. You will need to configure the reverse proxy for that. Django server is not designed to be run in production, so the additional configuration will be necessary when you release your site.cdvv7788

1 Answers

0
votes

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