uwsgi静态文件从django加载错误(反应和webpack)
uwsgi static files loading error form django (react and webpack)
我的应用程序由 django 和 react 组成,最后捆绑到静态 (.js) 文件。
我的目标是为它设置一个 nginx,但现在我只想在 uwsgi 中 运行 看看它是否有效。
Aaaa 它根本不起作用。我相信加载 webpack 正在编译的捆绑文件存在问题。
与 uwsgi 的连接正常,服务器已启动并且 运行ning,但是当连接到 127.0.0.1:8080 时,我得到(在 Mozilla 中):
The resource from “http://127.0.0.1:8080/static/frontend/main.js”
was blocked due to MIME type (“text/html”) mismatch
(X-Content-Type-Options: nosniff).
# and another
GET http://127.0.0.1:8080/static/frontend/main.js
HTTP/1.1 404 Not Found
Content-Type: text/html
X-Frame-Options: DENY
Content-Length: 3277
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Vary: Origin
我找到了类似的主题:Difference between static STATIC_URL and STATIC_ROOT on Django
还有这个:Django + React The resource was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)
但是弄乱 python manage.py collectstatic
什么也没解决。
我有这行代码来管理静态文件:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join('/app/label_it/front/static/frontend/')
这就是我启动 uwsgi 连接的方式:
python manage.py collectstatic --no-input
uwsgi --http 0.0.0.0:8080 --master --enable-threads --module label_it.wsgi
除了 collectstatic
之外,我还尝试过使用 <script src"static_file_path"></script>
这个静态路径:
<script src="../../static/frontend/main.js"></script>
# and
<script src="{% static "frontend/main.js" %}"></script>
没有任何结果。
感谢任何线索!
编辑:
我的文件结构:
好吧,我刚刚用我自己的服务器和 django 项目做了一些测试来证实我的怀疑,我得出以下结论:如果静态文件的路径不存在,就会发生这个错误。这意味着 uwsgi 无法使用您的设置找到此 main.js
文件。
我的建议是执行以下操作以确保以 Django 知道在哪里查找的方式正确提供静态文件。
默认情况下,django 在每个已安装的应用程序中搜索 static
文件夹以查找静态资产。因为您的项目不是这样设置的,所以您必须指定静态文件所在的其他目录。
这就是 STATICFILES_DIRS
的用途。
在你的 settings.py
中输入以下内容
STATICFILES_DIRS = [
BASE_DIR / 'label_it/front/static'
]
这将使 django 可以发现 label_it/front/static
中的所有文件。
现在这意味着
<script src="{% static "frontend/main.js" %}"></script>
会(希望)起作用。
此外,如果您还没有将静态网址包含在 urls.py
文件中,请记住。
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
...
]
# static content urls
urlpatterns += staticfiles_urlpatterns()
旁注:STATIC_ROOT
旨在成为您 运行 python manage.py collectstatic
.
时收集所有静态文件的位置
因此它应该是一个空目录,最好是在你的 django 项目的根目录中。
STATIC_ROOT = os.path.join(BASE_DIR, 'static_collected')
文件夹的名称并不重要,我以 static_collected
为例。
如果你 运行 命令你会看到 label_it/front/static/
中的所有文件都复制到那里。
我的应用程序由 django 和 react 组成,最后捆绑到静态 (.js) 文件。 我的目标是为它设置一个 nginx,但现在我只想在 uwsgi 中 运行 看看它是否有效。 Aaaa 它根本不起作用。我相信加载 webpack 正在编译的捆绑文件存在问题。
与 uwsgi 的连接正常,服务器已启动并且 运行ning,但是当连接到 127.0.0.1:8080 时,我得到(在 Mozilla 中):
The resource from “http://127.0.0.1:8080/static/frontend/main.js”
was blocked due to MIME type (“text/html”) mismatch
(X-Content-Type-Options: nosniff).
# and another
GET http://127.0.0.1:8080/static/frontend/main.js
HTTP/1.1 404 Not Found
Content-Type: text/html
X-Frame-Options: DENY
Content-Length: 3277
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Vary: Origin
我找到了类似的主题:Difference between static STATIC_URL and STATIC_ROOT on Django
还有这个:Django + React The resource was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)
但是弄乱 python manage.py collectstatic
什么也没解决。
我有这行代码来管理静态文件:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join('/app/label_it/front/static/frontend/')
这就是我启动 uwsgi 连接的方式:
python manage.py collectstatic --no-input
uwsgi --http 0.0.0.0:8080 --master --enable-threads --module label_it.wsgi
除了 collectstatic
之外,我还尝试过使用 <script src"static_file_path"></script>
这个静态路径:
<script src="../../static/frontend/main.js"></script>
# and
<script src="{% static "frontend/main.js" %}"></script>
没有任何结果。
感谢任何线索!
编辑:
我的文件结构:
好吧,我刚刚用我自己的服务器和 django 项目做了一些测试来证实我的怀疑,我得出以下结论:如果静态文件的路径不存在,就会发生这个错误。这意味着 uwsgi 无法使用您的设置找到此 main.js
文件。
我的建议是执行以下操作以确保以 Django 知道在哪里查找的方式正确提供静态文件。
默认情况下,django 在每个已安装的应用程序中搜索 static
文件夹以查找静态资产。因为您的项目不是这样设置的,所以您必须指定静态文件所在的其他目录。
这就是 STATICFILES_DIRS
的用途。
在你的 settings.py
中输入以下内容
STATICFILES_DIRS = [
BASE_DIR / 'label_it/front/static'
]
这将使 django 可以发现 label_it/front/static
中的所有文件。
现在这意味着
<script src="{% static "frontend/main.js" %}"></script>
会(希望)起作用。
此外,如果您还没有将静态网址包含在 urls.py
文件中,请记住。
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
...
]
# static content urls
urlpatterns += staticfiles_urlpatterns()
旁注:STATIC_ROOT
旨在成为您 运行 python manage.py collectstatic
.
因此它应该是一个空目录,最好是在你的 django 项目的根目录中。
STATIC_ROOT = os.path.join(BASE_DIR, 'static_collected')
文件夹的名称并不重要,我以 static_collected
为例。
如果你 运行 命令你会看到 label_it/front/static/
中的所有文件都复制到那里。