Django on Heroku - Broken Admin Static Files

前端 未结 9 504
天命终不由人
天命终不由人 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:18

    I got django admin working with following edits

    urls.py(at the end)

    import settings
    urlpatterns += patterns('',
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.STATIC_ROOT,
        }),
    )
    

    Procfile

    web: gunicorn hellodjango.wsgi -b 0.0.0.0:$PORT
    
    0 讨论(0)
  • 2020-12-24 15:18

    'django.contrib.staticfiles.views.serve'

    instead of

    'django.views.static.serve'

    0 讨论(0)
  • 2020-12-24 15:19

    If you use runserver and configure your app with DEBUG=True, then it will serve the admin files just like on your development machine. However, this is definitely not the recommended way to do it, and I would suggest that you put them on S3.

    Using the django-storages app it's very easy to configure collectstatic to automatically push all the admin files to S3. You can find directions here

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 15:23

    It seems little late compared to the asked date. But I got into this issue and spent 30 mins on what I did wrong. So here it is the magic solution for those who might fall in this trap.

    There is some problem with Heroku's django.contrib.staticfiles.urls

    SOLUTION

    You need to install dj-static (Link to download) on your Heroku setup. It's a Django middleware utility that allows to properly serve static assets from production with a WSGI server like Gunicorn.

    I hope this will help someone.

    0 讨论(0)
  • 2020-12-24 15:28

    Check out this post: http://matthewphiong.com/managing-django-static-files-on-heroku

    If that doesn't work for you try adding the following to your urls.py after the normal url pattern tuple. Make sure you have your STATIC_ROOT set and you've run collect static on your local environment all before pushing to heroku.

    urlpatterns += patterns('',
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.STATIC_ROOT,
        }),
     )
    
    0 讨论(0)
提交回复
热议问题