django server doesn't load css when it is run

I have all the static files, but for some reason only the normal html loads with no css. I have tried using collectstatic and creating a new project but it doesn't work on any. STATIC_URL is also specified along with static in the url.py . Nothing seems to be working.

1 answer

  • answered 2018-01-14 11:26 Dhaval Savalia

    This can happen if your DEBUG is set to False and did not mention any staticfiles dir. Okay, update your settings.py as below:

    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static_my_proj"),
    ]
    
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")
    

    Now, that you have done that make dir called static_my_proj, at the same level where your manage.py file is.

    Okay, now create new dir called static_cdn just one level up, i.e. one level up to manage.py file.

    Now, create dir called static_root inside static_cdn and that is all you have to do. Make sure you run python manage.py collectstatic

    NOTE:

    static_my_proj is for development or DEBUG = True and static_cdn is for production or DEBUG = True. handy, right?

    Also, you can add media file the same way, just add,

    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")
    

    and make new dir called media_root under static_cdn.

    Set DEBUG = True first and test it.

    Hope this helps.