Django on Heroku - Broken Admin Static Files

前端 未结 9 659
天命终不由人
天命终不由人 2020-12-24 14:37

I have a Django app running on Heroku/Cedar, configured as per the instructions at https://devcenter.heroku.com/articles/django

Using gunicorn as per Heroku\'s instr

9条回答
  •  太阳男子
    2020-12-24 15:20

    Follow this to fix all static related issues with Django and heroku.

    In your settings.py paste this at the end

    import os
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR,'static'),
        )
    TEMPLATE_DIRS = (
        os.path.join(BASE_DIR,'templates'),
        )
    
    STATIC_URL = '/static/'
    

    Your template for a particular app should be in app_name/templates/app_name/

    When you render template this is how you will specify template name

    in views.py

    .....
    return render(request,'app_name/template_name.html',context)
    

    For static files place your files here:

    project_folder/app_name/static/app_name/css

    project_folder/app_name/static/app_name/js

    project_folder/app_name/static/app_name/img

    to access your static file use path app_name/css/style_name.css

    If you follow this, all your static files will load up fine in heroku as well as in your local development machine.

提交回复
热议问题