Static files on OpenShift Django

前端 未结 5 1685
悲&欢浪女
悲&欢浪女 2020-12-16 06:59

I\'m having issues serving up static files - i.e. style sheets and images required from my html pages. I cannot seem to fathom why these static images are not being found. H

5条回答
  •  借酒劲吻你
    2020-12-16 07:29

    Hey I just had the same problem and I found a way to make it work.

    Just go to your settings.py and make some changes


    replace

    DJ_PROJECT_DIR = os.path.dirname(__file__)
    BASE_DIR = os.path.dirname(DJ_PROJECT_DIR)
    

    WITH

    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    

    down at the end of settings.py file add this

    STATIC_URL = '/static/'
    
    TEMPLATE_DIRS = [
    os.path.join(BASE_DIR, 'templates'),
    ]
    
    STATIC_ROOT = os.path.join(BASE_DIR, '../static')
    
    STATICFILES_DIRS = (
     os.path.join(BASE_DIR, 'static', 'dirs'),
    )
    

    and in your html file just don't forget to load staticfiles like that

    
     {% load staticfiles %}
    
     
             
             It works! 
    
    

    now your directories should look like that

    --wsgi
        --myproject
            --templates
                -- drop html files here ... 
            --static
                --dirs
                    --img
                        -- drop images here ..
                    --js
                        -- drop js here ..
                    --css
                        -- drop css files here ..
    

提交回复
热议问题