1

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.

4

1 回答 1

0

我需要设置 DEBUG = True 和 PRODUCTION = False 让它服务静态文件。只是 DEBUG = True 不会这样做。

于 2018-12-16T23:15:22.217 回答