Django 静态文件不显示在 Heroku 上
Django static files don't show up on 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'),
)
我通过添加
解决了这个问题
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT,'show_indexes': False}),
)
我的urls.py
结束
我几乎将我的 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'),
)
我通过添加
解决了这个问题urlpatterns += patterns('',
(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT,'show_indexes': False}),
)
我的urls.py