查看部署的 URL https://coach-matrix-d2cd1e717f81.herokuapp.com/ask_question/
这以前是一个问题记录在这里,但现在很多调试步骤似乎无关紧要,我已经得出了 Cloudinary 在我的 Django IDE 上正确安装的工作理论,而问题实际上在于 Cloudinary 和 Heroku 设置之间。
要点一览。
查看错误消息,它提到 MIME 生产版本的问题似乎是在本地(在 Heroku 上)而不是在 Cloudinary 上查找文件。
Refused to apply style from 'https://coach-matrix-d2cd1e717f81.herokuapp.com/static/django_quill/django_quill.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
django_quill.js:1
Failed to load resource: the server responded with a status of 404 (Not Found)
ask_question/:1 Refused to execute script from 'https://coach-matrix-d2cd1e717f81.herokuapp.com/static/django_quill/django_quill.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
ask_question/:108 Uncaught ReferenceError: QuillWrapper is not defined
at ask_question/:108:27
at ask_question/:110:11
ask_question/:1 Refused to apply style from 'https://coach-matrix-d2cd1e717f81.herokuapp.com/static/django_quill/django_quill.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
单击其中一个链接会导致此错误消息
Page not found (404)
Request Method: GET
Request URL: https://coach-matrix-d2cd1e717f81.herokuapp.com/static/django_quill/django_quill.js
Using the URLconf defined in coachmatrix.urls, Django tried these URL patterns, in this order:
admin/
index/ [name='index']
questions/ [name='questions']
ask_question/ [name='ask_question']
<slug:slug>/ [name='question_detail']
upvote/<slug:slug> [name='question_upvote']
downvote/<slug:slug> [name='question_downvote']
questions/<slug:slug>/upvote/ [name='question_upvote_from_list']
questions/<slug:slug>/downvote/ [name='question_downvote_from_list']
question/<int:pk>/delete/ [name='question_delete']
question/<slug:slug>/update/ [name='question_update']
answer/<slug:slug>/update/ [name='answer_update']
answer/<slug:slug>/delete/ [name='answer_delete']
answer/<int:pk>/upvote/ [name='answer_upvote']
answer/<int:pk>/downvote/ [name='answer_downvote']
accounts/
[name='home']
questions/ [name='questions']
accounts/
logout
The current path, static/django_quill/django_quill.js, didn’t match any of these.
我关于静态文件普遍问题的第一个线索是 CSS 在生产中也不起作用(同样在本地主机上也起作用)。它的标题是
<link rel="stylesheet" href="{% static 'css/style.css' %}"> <!--limited to localhost, not displaying in production-->
,必须更改为这个才能显示 CSS
<link rel="stylesheet" href="https://res.cloudinary.com/dh1xovduy/raw/upload/v1703781479/static/css/style.674a1e0f7f03.css"> <!--linking directly to the file now works successfully-->
如前所述,django settings.py STATIC_ROOT 等都工作正常,已经被几个同行审查过。它还使用collectstatic正确上传。问题肯定是在 Cloudinary 和 Heroku 之间。
确认来自cloudinary的静态文件,
0 static files copied, 1386 post-processed.
所有文件似乎已成功上传 Django-Quill CSS 和 JS 文件位于名为
django_quill
的文件夹中
版本控制末尾添加的语法可能会使 Heroku 无法读取文件。例如cloudinary 上的文件被称为
https://console.cloudinary.com/settings/
因此在库的设置中我使用
Unique filename: false
创建了一个新的上传方法
令人困惑的是,在manage.pycollectstatic之后,这似乎并没有改变文件类型,它甚至注册了一组未提交的文件,但在末尾添加了一个标签。
django_quill.557645779788.js
从末尾的随机字符串切换为资产的Etag值。
gitpod /workspace/Classroom-Matrix (main) $ python3 manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings.
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
[...]
0 static files copied, 1491 post-processed.
查看库,似乎根本没有覆盖现有文件
无论如何尝试部署但没有这样的运气。
似乎没有改变任何东西,无论是设置为1还是0
CLOUDINARY_URL=cloudinary:[已编辑]
DATABASE_URL=postgres: // [已编辑]
DISABLE_COLLECTSTATIC=0
端口=8000
SECRET_KEY= [已编辑]
我终于尝试在 Cloudinary 上手动删除名称字符串
django_quill.557645779788.js
被替换为
django_quill.js
这似乎也没有改变错误消息。
<link rel="stylesheet" href="{% static 'django_quill/django_quill.css' %}">
<script src="{% static 'django_quill/django_quill.js' %}"></script>
由于 CORS 问题,这甚至不会显示在本地主机中。步骤已恢复。
完全相同的错误消息。 DISABLE_COLLECTSTATIC 设置为 0。
发现该问题与缺少 Whitenoise 中间件有关,这是一个 Python 包,特别适合简化静态文件并让它们在部署的 Web 应用程序中提供服务。
尽管最初存在一些担忧,但 Cloudinary 或 Heroku 一直运行良好,并且可以通过一个非常简单的解决方案在代码存储库内部进行修复。
这是修复方法。
pip install whitenoise
在终端whitenoise==6.6.0
添加到 requirements.txt
settings.py
更新中间件MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # added whitenoise middleware here.
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'allauth.account.middleware.AccountMiddleware',
]
感谢代码学院校友社区的 Sean Mead 找到了拼图中的最后一块。