Django + React 资源因 MIME 类型(“text/html")不匹配(X-Content-Type-Options: nosniff)而被阻塞 [英] Django + React The resource was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)

查看:520
本文介绍了Django + React 资源因 MIME 类型(“text/html")不匹配(X-Content-Type-Options: nosniff)而被阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用前端 react 开发 django 应用程序并使用 django 提供 react 构建文件.项目结构是因为在 settings.py 中一切正常

Developing a django app with frontend react and serving the react build files with django. project structure is as everything works fine when in settings.py I have

DEBUG=True

但是当我将其更改为 DEBUG=False我在控制台中收到以下静态文件错误

However when I change it to DEBUG=False i get the following errors for static files in the console

资源来自http://localhost:8000/static/js/2.285a2b2f.chunk.js"被屏蔽了MIME 类型(text/html")不匹配(X-Content-Type-Options:nosniff).

The resource from "http://localhost:8000/static/js/2.285a2b2f.chunk.js" was blocked due to MIME type ("text/html") mismatch (X-Content-Type-Options: nosniff).

资源来自http://localhost:8000/static/css/main.dde91409.chunk.css"是由于 MIME 类型(text/html")不匹配而被阻止(X-Content-Type-Options:nosniff).

The resource from "http://localhost:8000/static/css/main.dde91409.chunk.css" was blocked due to MIME type ("text/html") mismatch (X-Content-Type-Options: nosniff).

资源来自http://localhost:8000/static/js/main.eade1a0b.chunk.js"是由于 MIME 类型(text/html")不匹配而被阻止(X-Content-Type-Options:nosniff).

The resource from "http://localhost:8000/static/js/main.eade1a0b.chunk.js" was blocked due to MIME type ("text/html") mismatch (X-Content-Type-Options: nosniff).

资源来自http://localhost:8000/static/css/2.03372da4.chunk.css"是由于 MIME 类型(text/html")不匹配而被阻止(X-Content-Type-Options:nosniff).

The resource from "http://localhost:8000/static/css/2.03372da4.chunk.css" was blocked due to MIME type ("text/html") mismatch (X-Content-Type-Options: nosniff).

资源来自http://localhost:8000/static/js/2.285a2b2f.chunk.js"是由于 MIME 类型(text/html")不匹配而被阻止(X-Content-Type-Options:nosniff).

The resource from "http://localhost:8000/static/js/2.285a2b2f.chunk.js" was blocked due to MIME type ("text/html") mismatch (X-Content-Type-Options: nosniff).

在我的 setting.py 文件中,我还设置了以下内容

in my setting.py file I have also set the following

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS':  [os.path.join(BASE_DIR, 'frontend')],
        '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_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'frontend', "build", "static"),  # update the STATICFILES_DIRS
)

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

一切正常,除非 DJango DEBUG 为 False请有人帮助我,因为我不想使用 DEBUG=True

Everything works fine except when DJango DEBUG is False Please can someone help me since I dont want to deploy the app with DEBUG=True

推荐答案

对于从 Google 来到这里的任何人,您可能应该看到 这个问题.

For anyone coming here from Google, you should probably see this question.

总而言之,当 Debug 关闭时,Django 将停止为您处理静态文件.您的生产服务器应该处理这个问题.

In summary, when Debug is off Django stops handling static files for you. Your production server should deal with this instead.

如果你仍然想在本地运行你的服务器,你应该使用:

If you still want to run your server locally, you should use:

./manage.py runserver --insecure

这在生产中显然不安全,因此得名.

This is obviously not secure in production, hence its name.

这篇关于Django + React 资源因 MIME 类型(“text/html")不匹配(X-Content-Type-Options: nosniff)而被阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆