Django:未找到静态/管理员/css
Posted
技术标签:
【中文标题】Django:未找到静态/管理员/css【英文标题】:Django: Not Found static/admin/css 【发布时间】:2017-01-10 12:00:45 【问题描述】:我刚刚在 Heroku 上部署了我的第一个 Django 应用程序,但我注意到它没有像我在本地计算机上运行服务器时那样的任何 CSS。我知道静态文件有问题,但即使我已经阅读了the docs,我也不太了解它。我可以的
python3 manage.py collectstatic
创建一个静态文件夹,但我不知道该放在哪里以及如何更改 settings.py 中的 DIRS。我真的需要一些帮助才能摆脱它。
settings.py:
DEBUG = True
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'household_management',
]
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS':
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
,
,
]
STATIC_ROOT = 'static'
STATIC_URL = '/static/'
heroku 日志:
2016-09-02T10:42:09.461124+00:00 heroku[router]: at=info method=GET path="/" host=peaceful-earth-63194.herokuapp.com request_id=33fc071d-344c-47e7-8721-919ba6d5df65 fwd="14.191.217.103" dyno=web.1 connect=2ms service=53ms status=302 bytes=400
2016-09-02T10:42:09.760323+00:00 heroku[router]: at=info method=GET path="/admin/login/?next=/" host=peaceful-earth-63194.herokuapp.com request_id=c050edcd-02d9-4c39-88ba-8a16be692843 fwd="14.191.217.103" dyno=web.1 connect=1ms service=45ms status=200 bytes=2184
2016-09-02T10:42:10.037370+00:00 heroku[router]: at=info method=GET path="/static/admin/css/login.css" host=peaceful-earth-63194.herokuapp.com request_id=ec43016a-09b7-499f-a84b-b8024577b717 fwd="14.191.217.103" dyno=web.1 connect=2ms service=9ms status=404 bytes=4569
2016-09-02T10:42:10.047224+00:00 heroku[router]: at=info method=GET path="/static/admin/css/base.css" host=peaceful-earth-63194.herokuapp.com request_id=6570ee02-3b78-44f4-9ab9-0e80b706ea40 fwd="14.191.217.103" dyno=web.1 connect=1ms service=16ms status=404 bytes=4566
2016-09-02T10:42:10.030726+00:00 app[web.1]: Not Found: /static/admin/css/login.css
2016-09-02T10:42:10.043743+00:00 app[web.1]: Not Found: /static/admin/css/base.css
2016-09-02T10:48:56.593180+00:00 heroku[api]: Deploy d1d39dc by huyvohcmc@gmail.com
2016-09-02T10:48:56.593290+00:00 heroku[api]: Release v21 created by huyvohcmc@gmail.com
2016-09-02T10:48:56.803122+00:00 heroku[slug-compiler]: Slug compilation started
2016-09-02T10:48:56.803127+00:00 heroku[slug-compiler]: Slug compilation finished
2016-09-02T10:48:56.893962+00:00 heroku[web.1]: Restarting
2016-09-02T10:48:56.894722+00:00 heroku[web.1]: State changed from up to starting
2016-09-02T10:48:59.681267+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-09-02T10:49:00.418357+00:00 app[web.1]: [2016-09-02 17:49:00 +0000] [9] [INFO] Worker exiting (pid: 9)
2016-09-02T10:49:00.418377+00:00 app[web.1]: [2016-09-02 17:49:00 +0000] [10] [INFO] Worker exiting (pid: 10)
2016-09-02T10:49:00.418393+00:00 app[web.1]: [2016-09-02 10:49:00 +0000] [3] [INFO] Handling signal: term
2016-09-02T10:49:00.477684+00:00 app[web.1]: [2016-09-02 10:49:00 +0000] [3] [INFO] Shutting down: Master
2016-09-02T10:49:00.594623+00:00 heroku[web.1]: Process exited with status 0
2016-09-02T10:49:00.607775+00:00 heroku[web.1]: Starting process with command `gunicorn assignment.wsgi --log-file -`
2016-09-02T10:49:02.911936+00:00 app[web.1]: [2016-09-02 10:49:02 +0000] [3] [INFO] Starting gunicorn 19.6.0
2016-09-02T10:49:02.912529+00:00 app[web.1]: [2016-09-02 10:49:02 +0000] [3] [INFO] Listening at: http://0.0.0.0:18162 (3)
2016-09-02T10:49:02.917427+00:00 app[web.1]: [2016-09-02 10:49:02 +0000] [9] [INFO] Booting worker with pid: 9
2016-09-02T10:49:02.912655+00:00 app[web.1]: [2016-09-02 10:49:02 +0000] [3] [INFO] Using worker: sync
2016-09-02T10:49:02.980208+00:00 app[web.1]: [2016-09-02 10:49:02 +0000] [10] [INFO] Booting worker with pid: 10
2016-09-02T10:49:04.228057+00:00 heroku[web.1]: State changed from starting to up
2016-09-02T10:53:41.572630+00:00 heroku[router]: at=info method=GET path="/" host=peaceful-earth-63194.herokuapp.com request_id=68c0b216-2084-46c8-9be5-b7e5aacaa590 fwd="14.191.217.103" dyno=web.1 connect=0ms service=42ms status=302 bytes=400
2016-09-02T10:53:41.880217+00:00 heroku[router]: at=info method=GET path="/admin/login/?next=/" host=peaceful-earth-63194.herokuapp.com request_id=17b91dc2-ba06-482c-8af0-e7b015fe2077 fwd="14.191.217.103" dyno=web.1 connect=0ms service=41ms status=200 bytes=2184
2016-09-02T10:53:42.156295+00:00 heroku[router]: at=info method=GET path="/static/admin/css/base.css" host=peaceful-earth-63194.herokuapp.com request_id=40dec62d-8c4a-4af6-8e0f-8053fe8379b9 fwd="14.191.217.103" dyno=web.1 connect=0ms service=9ms status=404 bytes=4566
2016-09-02T10:53:42.157491+00:00 heroku[router]: at=info method=GET path="/static/admin/css/login.css" host=peaceful-earth-63194.herokuapp.com request_id=3a29f200-c185-4344-a6e1-5af35e5d120e fwd="14.191.217.103" dyno=web.1 connect=0ms service=17ms status=404 bytes=4569
2016-09-02T10:53:42.164162+00:00 app[web.1]: Not Found: /static/admin/css/base.css
2016-09-02T10:53:42.177480+00:00 app[web.1]: Not Found: /static/admin/css/login.css
2016-09-02T11:01:19.031353+00:00 heroku[api]: Deploy 2beb15a by huyvohcmc@gmail.com
2016-09-02T11:01:19.031444+00:00 heroku[api]: Release v22 created by huyvohcmc@gmail.com
2016-09-02T11:01:19.262522+00:00 heroku[slug-compiler]: Slug compilation started
2016-09-02T11:01:19.262528+00:00 heroku[slug-compiler]: Slug compilation finished
2016-09-02T11:01:19.426837+00:00 heroku[web.1]: Restarting
2016-09-02T11:01:19.427455+00:00 heroku[web.1]: State changed from up to starting
2016-09-02T11:01:22.141325+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-09-02T11:01:22.545379+00:00 heroku[web.1]: Starting process with command `gunicorn assignment.wsgi --log-file -`
2016-09-02T11:01:22.754067+00:00 app[web.1]: [2016-09-02 18:01:22 +0000] [9] [INFO] Worker exiting (pid: 9)
2016-09-02T11:01:22.754077+00:00 app[web.1]: [2016-09-02 18:01:22 +0000] [10] [INFO] Worker exiting (pid: 10)
2016-09-02T11:01:22.757599+00:00 app[web.1]: [2016-09-02 11:01:22 +0000] [3] [INFO] Handling signal: term
2016-09-02T11:01:22.763197+00:00 app[web.1]: [2016-09-02 11:01:22 +0000] [3] [INFO] Shutting down: Master
2016-09-02T11:01:22.880977+00:00 heroku[web.1]: Process exited with status 0
2016-09-02T11:01:24.628348+00:00 app[web.1]: [2016-09-02 11:01:24 +0000] [3] [INFO] Starting gunicorn 19.6.0
2016-09-02T11:01:24.628921+00:00 app[web.1]: [2016-09-02 11:01:24 +0000] [3] [INFO] Listening at: http://0.0.0.0:34235 (3)
2016-09-02T11:01:24.629075+00:00 app[web.1]: [2016-09-02 11:01:24 +0000] [3] [INFO] Using worker: sync
2016-09-02T11:01:24.636198+00:00 app[web.1]: [2016-09-02 11:01:24 +0000] [9] [INFO] Booting worker with pid: 9
2016-09-02T11:01:24.722355+00:00 app[web.1]: [2016-09-02 11:01:24 +0000] [10] [INFO] Booting worker with pid: 10
2016-09-02T11:01:26.271435+00:00 heroku[web.1]: State changed from starting to up
2016-09-02T11:01:27.930795+00:00 heroku[router]: at=info method=GET path="/" host=peaceful-earth-63194.herokuapp.com request_id=a844ef4b-a2d1-44fe-af0e-09c76cb0e034 fwd="14.191.217.103" dyno=web.1 connect=0ms service=46ms status=302 bytes=400
2016-09-02T11:01:28.363163+00:00 heroku[router]: at=info method=GET path="/admin/login/?next=/" host=peaceful-earth-63194.herokuapp.com request_id=31c0823a-466f-4363-b550-3c81681305f5 fwd="14.191.217.103" dyno=web.1 connect=0ms service=171ms status=200 bytes=2184
2016-09-02T11:01:28.716801+00:00 heroku[router]: at=info method=GET path="/static/admin/css/base.css" host=peaceful-earth-63194.herokuapp.com request_id=2d1b8bb2-9ab3-49f7-b557-a54eed996547 fwd="14.191.217.103" dyno=web.1 connect=0ms service=8ms status=404 bytes=4566
2016-09-02T11:01:28.693936+00:00 heroku[router]: at=info method=GET path="/static/admin/css/login.css" host=peaceful-earth-63194.herokuapp.com request_id=24aa1eed-aa87-4854-ab35-1604e8393b9d fwd="14.191.217.103" dyno=web.1 connect=0ms service=18ms status=404 bytes=4569
2016-09-02T11:01:28.681948+00:00 app[web.1]: Not Found: /static/admin/css/base.css
2016-09-02T11:01:28.692958+00:00 app[web.1]: Not Found: /static/admin/css/login.css
2016-09-02T11:12:43.686922+00:00 heroku[api]: Deploy 63085e6 by huyvohcmc@gmail.com
2016-09-02T11:12:43.687037+00:00 heroku[api]: Release v23 created by huyvohcmc@gmail.com
2016-09-02T11:12:43.951987+00:00 heroku[slug-compiler]: Slug compilation started
2016-09-02T11:12:43.951998+00:00 heroku[slug-compiler]: Slug compilation finished
2016-09-02T11:12:43.926959+00:00 heroku[web.1]: Restarting
2016-09-02T11:12:43.929107+00:00 heroku[web.1]: State changed from up to starting
2016-09-02T11:12:46.931285+00:00 heroku[web.1]: Starting process with command `gunicorn assignment.wsgi --log-file -`
2016-09-02T11:12:47.860591+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-09-02T11:12:48.729601+00:00 app[web.1]: [2016-09-02 18:12:48 +0000] [10] [INFO] Worker exiting (pid: 10)
2016-09-02T11:12:48.729617+00:00 app[web.1]: [2016-09-02 18:12:48 +0000] [9] [INFO] Worker exiting (pid: 9)
2016-09-02T11:12:48.729623+00:00 app[web.1]: [2016-09-02 11:12:48 +0000] [3] [INFO] Handling signal: term
2016-09-02T11:12:48.775112+00:00 app[web.1]: [2016-09-02 11:12:48 +0000] [3] [INFO] Shutting down: Master
2016-09-02T11:12:48.890301+00:00 heroku[web.1]: Process exited with status 0
2016-09-02T11:12:48.839674+00:00 app[web.1]: [2016-09-02 11:12:48 +0000] [3] [INFO] Starting gunicorn 19.6.0
2016-09-02T11:12:48.840093+00:00 app[web.1]: [2016-09-02 11:12:48 +0000] [3] [INFO] Listening at: http://0.0.0.0:20001 (3)
2016-09-02T11:12:48.840166+00:00 app[web.1]: [2016-09-02 11:12:48 +0000] [3] [INFO] Using worker: sync
2016-09-02T11:12:48.843687+00:00 app[web.1]: [2016-09-02 11:12:48 +0000] [9] [INFO] Booting worker with pid: 9
2016-09-02T11:12:48.939210+00:00 app[web.1]: [2016-09-02 11:12:48 +0000] [10] [INFO] Booting worker with pid: 10
2016-09-02T11:12:50.565750+00:00 heroku[web.1]: State changed from starting to up
2016-09-02T11:13:00.439745+00:00 heroku[router]: at=info method=GET path="/" host=peaceful-earth-63194.herokuapp.com request_id=c30b47e6-fbb8-4412-9242-5fe37217026a fwd="14.191.217.103" dyno=web.1 connect=0ms service=49ms status=400 bytes=199
2016-09-02T11:14:01.686661+00:00 heroku[api]: Deploy c149525 by huyvohcmc@gmail.com
2016-09-02T11:14:01.686965+00:00 heroku[api]: Release v24 created by huyvohcmc@gmail.com
2016-09-02T11:14:02.189063+00:00 heroku[slug-compiler]: Slug compilation started
2016-09-02T11:14:02.189073+00:00 heroku[slug-compiler]: Slug compilation finished
2016-09-02T11:14:02.466456+00:00 heroku[web.1]: Restarting
2016-09-02T11:14:02.467005+00:00 heroku[web.1]: State changed from up to starting
2016-09-02T11:14:04.713176+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-09-02T11:14:05.259388+00:00 app[web.1]: [2016-09-02 18:14:05 +0000] [10] [INFO] Worker exiting (pid: 10)
2016-09-02T11:14:05.260345+00:00 app[web.1]: [2016-09-02 11:14:05 +0000] [3] [INFO] Handling signal: term
2016-09-02T11:14:05.265937+00:00 app[web.1]: [2016-09-02 18:14:05 +0000] [9] [INFO] Worker exiting (pid: 9)
2016-09-02T11:14:05.317647+00:00 app[web.1]: [2016-09-02 11:14:05 +0000] [3] [INFO] Shutting down: Master
2016-09-02T11:14:05.411311+00:00 heroku[web.1]: Process exited with status 0
2016-09-02T11:14:06.581314+00:00 heroku[web.1]: Starting process with command `gunicorn assignment.wsgi --log-file -`
2016-09-02T11:14:10.282506+00:00 heroku[web.1]: State changed from starting to up
2016-09-02T11:14:10.187781+00:00 app[web.1]: [2016-09-02 11:14:10 +0000] [3] [INFO] Starting gunicorn 19.6.0
2016-09-02T11:14:10.188490+00:00 app[web.1]: [2016-09-02 11:14:10 +0000] [3] [INFO] Listening at: http://0.0.0.0:27446 (3)
2016-09-02T11:14:10.188627+00:00 app[web.1]: [2016-09-02 11:14:10 +0000] [3] [INFO] Using worker: sync
2016-09-02T11:14:10.211822+00:00 app[web.1]: [2016-09-02 11:14:10 +0000] [9] [INFO] Booting worker with pid: 9
2016-09-02T11:14:10.231978+00:00 app[web.1]: [2016-09-02 11:14:10 +0000] [10] [INFO] Booting worker with pid: 10
2016-09-02T11:14:29.714607+00:00 heroku[router]: at=info method=GET path="/" host=peaceful-earth-63194.herokuapp.com request_id=947ed6b9-b48a-48b1-8860-36846248acea fwd="14.191.217.103" dyno=web.1 connect=0ms service=153ms status=302 bytes=400
2016-09-02T11:14:30.522664+00:00 heroku[router]: at=info method=GET path="/admin/login/?next=/" host=peaceful-earth-63194.herokuapp.com request_id=b74c55bf-913c-4e0d-8d16-2b1f4f0cea13 fwd="14.191.217.103" dyno=web.1 connect=0ms service=561ms status=200 bytes=2184
2016-09-02T11:14:30.879732+00:00 heroku[router]: at=info method=GET path="/static/admin/css/base.css" host=peaceful-earth-63194.herokuapp.com request_id=769f989a-f051-4a89-a079-1d6acea3c185 fwd="14.191.217.103" dyno=web.1 connect=0ms service=86ms status=404 bytes=4566
2016-09-02T11:14:30.865971+00:00 heroku[router]: at=info method=GET path="/static/admin/css/login.css" host=peaceful-earth-63194.herokuapp.com request_id=b271b831-a4fb-4bdb-9f6a-e4d66297db88 fwd="14.191.217.103" dyno=web.1 connect=0ms service=75ms status=404 bytes=4569
2016-09-02T11:14:30.865501+00:00 app[web.1]: Not Found: /static/admin/css/login.css
2016-09-02T11:14:30.871110+00:00 app[web.1]: Not Found: /static/admin/css/base.css
【问题讨论】:
django
不支持在生产环境中提供静态文件。看看这个:devcenter.heroku.com/articles/django-assets
【参考方案1】:
您还需要将静态目录添加到您的urls.py
文件中。添加以下内容:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
【讨论】:
当我将static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
添加到 urlpatterns 时,我遇到了这个错误:(urls.E004) Your URL pattern [] is invalid. Ensure that urlpatterns is a list of path() and/or re_path() instances.
@BenyaminJafari 这意味着您的 urlpatterns
列表为空。检查您的语法并再次尝试一下。看来您实际上并未将 static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
添加到列表中。
不,urlpatterns
列表不为空,语法没有任何问题。这意味着urlspattern
仅支持path()
和re_path()
不支持static()
。无论如何,我的问题通过另一种方式解决了。
static()
返回一个 re_path
对象 :) 来源:github.com/django/django/blob/master/django/conf/urls/static.py【参考方案2】:
你不应该改变BASE_DIR
在settings.py
中编辑STATIC_ROOT
的值
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
然后再次运行collectstatic
【讨论】:
我跑了collectstatic
,现在我应该把static
文件夹放在哪里?
你不要把它放在任何地方。如果命令成功运行,您应该在项目的根目录中有一个名为 static
的文件夹。它应该包含您的 css 和 js,并且应该从那里提供这些文件。您可能需要进一步配置服务器或更改模板以提供静态数据。按照其他人的建议,查看 Heroku 关于该主题的文档
从 docker 运行时,我很难让它工作。这篇博文对我非常有帮助:testdriven.io/blog/…【参考方案3】:
我遇到了同样的问题,但没有一个答案对我不起作用。通过这种方式,我找到了解决这个问题的解决方案:
我使用了STATICFILES_DIRS
而不是STATIC_ROOT
,并且调试也应该设置为true:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
DEBUG = True
如果您为静态文件使用子文件夹(即front-end
),您可以使用以下过程:
from unipath import Path
BASE_DIR = Path(__file__).ancestor(2)
# STATIC_ROOT = os.path.join(BASE_DIR.child('front-end'), "static")
STATICFILES_DIRS = [
os.path.join(BASE_DIR.child('front-end'), "static"),
]
DEBUG = True
[注意]:
Django 2.2 Python 3.6【讨论】:
【参考方案4】:Django 在生产环境中不提供静态文件。通常,要在生产中提供 Django 静态文件,您需要设置一个独立服务器,例如nginx。
但是,在 Heroku 中提供静态文件的方式有点不同。有关如何在 Heroku 中提供静态文件的详细信息,请参阅 Heroku 团队提供的以下链接:https://devcenter.heroku.com/articles/django-assets
编辑:使答案符合 *** 指南:
按照 Heroku 提供静态文件的指南:
在settings.py中添加:
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
然后使用以下命令安装 WhiteNoise 项目:
$ pip install whitenoise
在你的 wsgi.py 中:
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
【讨论】:
【参考方案5】:截至今天的有效解决方案 版本:
Django==3.1.3
Python 3.6.9
有效的设置:
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, "static")
# STATIC_DIR = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
DEBUG = True
所以请去掉 STATIC_ROOT
和 STATIC_DIR
并改用 STATICFILES_DIRS
。遗憾的是,这只适用于 DEBUG = True,意思是,它不适用于生产。 django 多年来一直存在这个静态文件问题,不幸的是,django 对他们的框架如此自以为是,似乎无法为我们提供一个在生产和开发环境中开箱即用的工作意见。
【讨论】:
【参考方案6】:在settings.py中
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
然后运行
python manage.py collectstatic
如果问题仍然存在,请在项目选项卡上右键单击浏览器,然后导航到检查元素,然后找到网络选项卡并启用“禁用缓存”
【讨论】:
以上是关于Django:未找到静态/管理员/css的主要内容,如果未能解决你的问题,请参考以下文章