python - Django 服务构建有许多 MIME 类型错误(sveltekit)

标签 python html django svelte sveltekit

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="icon" href="/static/icons/tabIcon.svg" />
    <link rel="stylesheet" href="/static/posty.css" />

    <meta http-equiv="content-security-policy" content="" />
    <link
      rel="modulepreload"
      href="/static/_app/immutable/start-6f351e07.js"
    />
    <link
      rel="modulepreload"
      href="/static/_app/immutable/chunks/index-d9a79891.js"
    />
    <link
      rel="modulepreload"
      href="/static/_app/immutable/chunks/index-d827f58a.js"
    />
    <link
      rel="modulepreload"
      href="/static/_app/immutable/chunks/singletons-eca981c1.js"
    />
  </head>
  <body>
    <div>
      <script type="module" data-sveltekit-hydrate="45h" crossorigin>
        import {
          set_public_env,
          start,
        } from "/static/_app/immutable/start-6f351e07.js";

        set_public_env({});

        start({
          target: document.querySelector('[data-sveltekit-hydrate="45h"]')
            .parentNode,
          paths: { base: "", assets: "/static/" },
          session: {},
          route: true,
          spa: true,
          trailing_slash: "never",
          hydrate: null,
        });
      </script>
    </div>
  </body>
</html>

svelte.config.js

import adapter from '@sveltejs/adapter-static';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    preprocess: preprocess(),

    kit: {
        adapter: adapter({
            assets: 'build/assets',
            out: 'dist',
            fallback: 'index.html',
            precompress: false
        })
    }
};

export default config;

设置.py
# ...
DEBUG = True

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'build/assets')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'build')],
        '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',
            ],
        },
    },
]
# ...

错误

The resource from “http://127.0.0.1:8000/_app/immutable/pages/__layout.svelte-bd484656.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
The resource from “http://127.0.0.1:8000/_app/immutable/assets/__layout-ce6f71c3.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
The resource from “http://127.0.0.1:8000/_app/immutable/chunks/index-d9a79891.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
... 9+ more errors

描述

即使使用 python manage.py runserver --nostatic --insecure(使用 whitenoise)仍然会导致此错误,并且在构建生产版本后我必须更改 中的所有 url index.html/static/ 开头,否则将找不到任何文件,有没有办法在部署期间修复或自动化,有没有更好的方法来做到这一点?谢谢!

最佳答案

在 Django 中一个更好的方法是在开始时加载静态

{% load static %}
<!DOCTYPE html>

并使用静态模板标签为给定的相对路径构建 URL,使用配置的 STATICFILES_STORAGE , 喜欢

<link href="{% static "css/base.css" %}" rel="stylesheet">

这样,如果您更改静态 URL,则不必在模板的多个位置进行更改。 Read more about it here .

作为引用,在脚本标签内可以使用 json_script . Read more about it here .

关于python - Django 服务构建有许多 MIME 类型错误(sveltekit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73483956/

相关文章:

python - 使用 BeautifulSoup 抓取 Iframe

python - 使用Django传递数据时如何在html中的<p>标签内添加新行?

javascript - React - 左键单击未触发 onMouseDown/onMouseUp

django - 自动搭建Django开发环境

python - Willie python irc bot - 获取 channel 上的昵称列表

python - 如何使 Plotly 图形动画工作

python - MYSQL 和 Python(通过 ssh)

javascript - 在所有内容上显示 div

python - Django:基于类的 View 、URL 和 template_name

Django:信号接收器可以返回数据吗?