Python 如何在django中正确服务静态文件?论赫罗库

Python 如何在django中正确服务静态文件?论赫罗库,python,django,heroku,Python,Django,Heroku,我的静态文件在DEBUG=True条件下工作,但在DEBUG=False条件下不工作 我的设置.py STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') url.py from django.conf.urls.static import static from dja

我的静态文件在DEBUG=True条件下工作,但在DEBUG=False条件下不工作

我的设置.py

STATIC_URL = '/static/'


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
url.py

from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
#....
] + static(settings.MEDIA_URL , document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
命令我试试 python manage.py静态 它创建了一个文件夹,但无法呈现数据

home.html

<!DOCTYPE html>
{% load staticfiles %} # I try {% load static %} also
<html>
<head>
   <link rel="stylesheet" type="text/css" href="{% static 'app1css/home.css' %}">
      <title></title>
  </head>
  <body>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do 
     .</p>
     </body>
   </html>
我读过许多与此相关的问题,但无法解决我的问题。我已经用这个设置(静态文件)将我的博客上传到heroku,但是它给了我SERVERERROR 500(debug=False)。我已尝试将{%load static%}更改为{%load staticfiles%},但仍然

django版本2.1.5 服务器apache
python 3.6.7版

您使用的是
whitenoise
?我已经安装了它,但没有使用。您应该使用它。您是否将其包括在中间件设置中?。您应该将
whitenoise.middleware.WhiteNoiseMiddleware
添加到
SecurityMiddleware
的正下方。是的,很好吃!!谢谢@Sergio和houzayfa rifai非常感谢你们救了我的命你们在使用Heroku的免费层吗?如果是这样,文件只存储有限的时间,因此对于数据,您需要使用postgresql数据库。提供实现该功能的必要步骤。关于这些文件,我恐怕没有办法无限期地将它们存储在Herokus中(对于免费层)。您使用的是
whitenoise
?我已经安装了,但没有使用。您应该使用它。您是否将其包括在中间件设置中?。您应该将
whitenoise.middleware.WhiteNoiseMiddleware
添加到
SecurityMiddleware
的正下方。是的,很好吃!!谢谢@Sergio和houzayfa rifai非常感谢你们救了我的命你们在使用Heroku的免费层吗?如果是这样,文件只存储有限的时间,因此对于数据,您需要使用postgresql数据库。提供实现该功能的必要步骤。关于这些文件,恐怕没有办法无限期地将它们存储在Herokus中(对于免费层)。
C:.
├───app1
│   ├───migrations
│       │   └───__pycache__
│           └───__pycache__
├───mysite
│   ├───static
│   │   └───app1css
│   └───__pycache__
├───static
│   ├───admin
│   │   ├───css
│   │   │   └───vendor
│   │   │       └───select2
│   │   ├───fonts
│   │   ├───img
│   │   │   └───gis
│   │   └───js
│   │       ├───admin
│   │       └───vendor
│   │           ├───jquery
│   │           ├───select2
│   │           │   └───i18n
│   │           └───xregexp
│   └───app1css
└───templates
    └───app