Tap*_*hak 1 python django heroku django-staticfiles
我正在尝试将我的Django项目推送到Heroku,但它没有加载静态文件.
我用它来设置东西,一切都很好,但我无法解决静态文件的问题.
我的目录结构是这样的
help_the_needy
help_the_needy
__init__.py
settings.py
urls.py
views.py
wsgi.py
manage.py
Procfile
requirements.txt
static
css
font-awesome
fonts
img
js
templates
base.html
display_list2.html
index.html
Run Code Online (Sandbox Code Playgroud)
这是完整的代码(所有文件).
这是我的settings.py.
我尝试了很多东西来解决这个问题,但似乎没有任何工作.
当我按下它时会复制静态文件,但它不会加载它们.
有人可以指出我的错误吗?哪里错了?
我一直在处理同样的问题.以下是我在代码中更改的两件事.
(我正在使用Django 1.7)
1)settings.py
我将这些行添加到设置文件中
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
# Add to this list all the locations containing your static files
)
Run Code Online (Sandbox Code Playgroud)
STATIC_ROOT:这告诉Django在哪里(a)在运行时放置静态文件python manage.py collectstatic
和(b)在运行应用程序时找到静态文件
TEMPLATE_DIRS:这告诉Django在运行时搜索静态文件时在哪里查找静态文件 python manage.py collectstatic
2)wsgi.py
最初我的文件是:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxx.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Run Code Online (Sandbox Code Playgroud)
我把它改成了:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxx.settings")
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
Run Code Online (Sandbox Code Playgroud)
有关whitenoise的更多信息,请阅读此处:https://devcenter.heroku.com/articles/django-assets#whitenoise
另外,请记住安装whitenoise:
pip install whitenoise==2.0.6
在部署项目之前,运行:
python manage.py collectstatic
这将创建一个由STATIC_ROOT指示的文件夹(在settings.py中声明),其中包含所有静态文件.
归档时间: |
|
查看次数: |
4691 次 |
最近记录: |