Django:拒绝应用...的样式,因为其 MIME 类型('text/html')不是受支持的样式表 MIME 类型

问题描述 投票:0回答:3

我正在尝试使用静态文件{% static 'assets/css/style.css' %}在django模板中连接我的

style.css
,但我一直看到这个错误
Refused to apply style from 'http://127.0.0.1:8000/assets/css/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
注意:当我复制 css 并将其手动粘贴到该部分内的样式标记中时,一切正常,但我的 css 有超过 23,000 行代码,放在项目的头部部分太多了。请问我该如何修复这个错误。

index.html

{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Favicon -->
    <link rel="shortcut icon" href="{% static 'assets/images/favicon.ico' %}">

    <!-- Google Font -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Heebo:wght@400;500;700&family=Roboto:wght@400;500;700&display=swap">

    <!-- Plugins CSS -->
    <link rel="stylesheet" type="text/css" href="{% static 'assets/vendor/font-awesome/css/all.min.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'assets/vendor/bootstrap-icons/bootstrap-icons.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'assets/vendor/tiny-slider/tiny-slider.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'assets/vendor/glightbox/css/glightbox.css' %}">

    <!-- Theme CSS -->
    <link id="style-switch" rel="stylesheet" type="text/css" href="{% static '/assets/css/style.css' %}">
</head>

├───base
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───course
│   ├───migrations
│   │   └───__pycache__
│   ├───templatetags
│   │   └───__pycache__
│   └───__pycache__
├───dashboard
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───dexxaedprj
│   └───__pycache__
├───static
│   ├───assets
│   │   ├───css
│   │   │   ├───components
│   │   │   │   └───vendor
│   │   │   └───custom
│   │   │       ├───forms
│   │   │       └───helper
│   │   ├───images
│   │   │   ├───avatar
│   │   │   ├───client
│   │   │   ├───courses
│   │   │   │   └───4by3
│   │   │   ├───element
│   │   │   └───flags
│   │   ├───js
│   │   └───vendor
│   │       ├───bootstrap
│   │       │   ├───dist
│   │       │   │   └───js
│   │       │   ├───js
│   │       │   │   └───src
│   │       │   │       ├───dom
│   │       │   │       └───util
│   │       │   ├───node_modules
│   │       │   │   └───@popperjs
│   │       │   │       └───core
│   │       │   │           └───lib
│   │       │   │               ├───dom-utils
│   │       │   │               ├───modifiers
│   │       │   │               └───utils
│   │       │   └───scss
│   │       │       ├───forms
│   │       │       ├───helpers
│   │       │       ├───mixins
│   │       │       ├───utilities
│   │       │       └───vendor
│   │       ├───bootstrap-icons
│   │       │   └───fonts
│   │       ├───font-awesome
│   │       │   ├───css
│   │       │   └───webfonts
│   │       ├───glightbox
│   │       │   ├───css
│   │       │   └───js
│   │       ├───purecounterjs
│   │       │   └───dist
│   │       └───tiny-slider
│   └───Old Assets
│       └───assets
│           ├───css
│           ├───images
│           │   ├───about
│           │   ├───course-images
│           │   ├───courses
│           │   ├───dashboard
│           │   └───left-imgs
│           ├───js
│           └───vendor
│               ├───bootstrap
│               │   ├───css
│               │   │   └───dist
│               │   │       └───css
│               │   └───js
│               ├───fontawesome-free
│               │   ├───css
│               │   └───webfonts
│               ├───jquery-ui-1.12.1
│               ├───js
│               │   └───src
│               │       └───tools
│               ├───node_modules
│               │   └───popper.js
│               │       └───dist
│               │           └───esm
│               ├───OwlCarousel
│               │   └───assets
│               ├───scss
│               │   ├───mixins
│               │   ├───utilities
│               │   └───vendor
│               ├───semantic
│               └───unicons-2.0.1
│                   ├───css
│                   └───fonts
├───templates
│   ├───base
│   └───Old Templates
│       ├───admin
│       ├───base
│       ├───course
│       ├───dashboard
│       ├───design
│       ├───howto
│       └───userauths
└───userauths
    ├───migrations
    │   └───__pycache__
    └───__pycache__
javascript python django django-rest-framework django-templates
3个回答
2
投票

我所做的就是在我的 base.html 头部添加这一行

<head>
     ...
     <base href="{% static '/' %}">
</head>

2
投票

django.contrib.staticfiles 提供了一个方便的管理命令,用于在单个目录中收集静态文件,以便您可以轻松地提供它们。

  1. 将 STATIC_ROOT 设置设置为您要从中提供这些文件的目录,例如:

STATIC_ROOT =“/var/www/example.com/static/”

2.运行collectstatic管理命令:

$ python 管理.py 收集静态

这会将静态文件夹中的所有文件复制到 STATIC_ROOT 目录中。

参考:https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-static-files-during-development


0
投票

我有同样的错误,并且这个解决方案似乎不起作用。我仍然遇到同样的错误。 知道为什么会发生这种情况以及如何解决它吗? 非常感谢大家!

© www.soinside.com 2019 - 2024. All rights reserved.