Using static files with the django virtual server

前端 未结 2 1944
無奈伤痛
無奈伤痛 2020-12-04 03:07

Questions exactly like this have been asked before, i\'ve read them all and tried to make sense of the official django documentation on the subject but i\'m still struggling

2条回答
  •  甜味超标
    2020-12-04 03:34

    You should write your settings.py like that:

    import os
    PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    
    STATIC_URL = '/static/'  # Should be root relative or absolute
    
    STATICFILES_DIRS = (
        os.path.join(PROJECT_ROOT, 'static'),
    )
    
    # Where your static files will be collected with colletstatic command
    # I use it only in deployed configuration with DEBUG=False
    # Need to be outside STATICFILES_DIR
    STATIC_ROOT = '/var/www/your.site' 
    

    Just to be sure, check with Firebug or webkit debugger that CSS is loaded.

    Remember that Django won't serve your static files if settings.DEBUG == False.

提交回复
热议问题