Django静态文件不会出现在Heroku上

时间:2015-02-05 03:30:46

标签: python django heroku

我差点将我的django项目部署到heroku,但我的静态文件没有显示出来。

|--- .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

当我在本地运行项目时,我的所有静态文件都使用退出代码200来访问。

但是,在部署到heroku时找不到它们。 collectstatic是成功的。

以下是我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"),
)

有没有人见过这个?

修改

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'),
)

1 个答案:

答案 0 :(得分:0)

我通过添加

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

到我urls.py

的末尾