You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path

前端 未结 3 1406
感情败类
感情败类 2021-01-12 16:51

I have gone through every other answer and cannot seem to get anything to work for me. I am following a tutorial and trying to push my master branch to heroku a

相关标签:
3条回答
  • 2021-01-12 17:20

    Summarizing ratrace's answer, the main solution is to add

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    

    to settings.py. Where STATIC_ROOT is the destination to where static files are copied and from where they are served when deploying the Django app.

    0 讨论(0)
  • 2021-01-12 17:28

    On this website it says to add this url pattern to your project:

    urlpatterns += patterns(”, (r’^static/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: settings.STATIC_ROOT}),)
    

    I haven't used heroku before but IF I understand correctly, it needs a path to send all the collected static files to. Usually that's in the server's settings. But the website above mentions that heroku uses an app which doesn't support static files deployment, and that's why you need a url pattern to make it get the static files from their respective places.

    Also, read the entire tutorial first. They might mention your problem somewhere near the end of the tutorial in like a Notes section. Read the comments also if they exist.

    0 讨论(0)
  • 2021-01-12 17:41

    I was able to make this work by getting rid of the if statement all together and just having the following in my settings.py.

    import dj_database_url
    DATABASES = {
        'default': dj_database_url.config(default='postgres://localhost')
    }
    
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    
    ALLOWED_HOSTS = ['*']
    
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )
    
    0 讨论(0)
提交回复
热议问题