简体   繁体   中英

Django static files don't show up on Heroku

I've nearly got my django project deployed to heroku, but my static files aren't showing up.

|--- .git
|--- .gitignore
|--- Procfile
|--- requirements.txt
|--- manage.py
|--- myproj
    |--- __init__.py
    |--- settings.py
    |--- urls.py
    |--- wsgi.py
    |--- templates
         |--- *.html
    |--- static
    |--- media
    |--- static
        |--- js
            |--- *.js
        |--- css
            |--- *.css
    |--- static-only
|--- myapp

When I run my project locally, all of my static files are accessed with exit code 200.

However, they are not located when deploying to heroku. collectstatic is successful.

Below is the relevant part of my settings.py

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

STATIC_URL = '/static/'

STATIC_ROOT = 'staticfiles'
MEDIA_ROOT = 'media'

# Template location

TEMPLATE_DIRS = (
   os.path.join(os.path.dirname(BASE_DIR), "templates"),
)

STATICFILES_DIRS = (
   os.path.join(os.path.dirname(BASE_DIR), "static"),
)

Has anyone seen this before?

EDIT

myproj/urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
import settings

urlpatterns = (
    url(r'^$', 'myapp.views.home', name='home'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^auth/$', 'myapp.views.auth_view'),
    url(r'^logout/$', 'myapp.views.logout'),
    url(r'^loggedin/$', 'myapp.views.loggedin'),
    url(r'invalid/$', 'myapp.views.invalid_login'),
    url(r'^register/', 'myapp.views.register_user'),
    url(r'^register_success/', 'myapp.views.register_success'),
    url(r'^files/', 'myapp.views.files'),
    url(r'^practice/', 'myapp.views.practice'),
    url(r'^download/', 'myapp.views.download'),
    url(r'^listening/', 'myapp.views.listening'),
    url(r'^training/', 'myapp.views.training'),
    url(r'^upload/', 'myapp.views.upload'),
    url(r'^update_user/', 'myapp.views.update_user'),
)

I fixed this by adding

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

to the end of my urls.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM