Продолжаем получать ошибку MIME при попытке прикрепить css файл Django
Я знаю, что этот вопрос уже задавался раньше, но я пытался реализовать ответы на этих форумах, но пока ни один из них не сработал для меня. Вот моя папка: Files. Вот мой html файл:
{% include 'main.html' %}
{% load static %}
<head>
<link rel="stylesheet" type="text/css" href="projects.css"/>
</head>
<body>
<h1 style="text-align: center">Projects</h1>
<h1>{{projects}}</h1>
{% for project in context %}
{% with 'jweb/images/'|add:project.image as project_photo %}
<div class="card-wrap">
<div class="card">
<h2 class="card-header">{{project.title}}</h2>
<div class="card-image">
<img src="{% static project_photo %}">
</div>
</div>
</div>
{% endwith %}
{% endfor %}
</body>
{% include 'pagebottom.html' %}
Вот мой css:
.card-wrap{
width: auto;
}
.card{
background: blue;
padding:3rem;
border:none;
box-shadow: 0 2px 5px 0 rgb(0,0,.2);
border-radius: .25rem;
}
.card-image{
min-width: 0;
min-width: 0;
}
.card-image > img{
height:100%;
width:100%;
object-fit:contain;
}
Вот мой settings.py:
Я постоянно получаю ошибку 404: Refused to apply style from 'http://127.0.0.1:8000/projects/projects.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled. Похоже, что я правильно указал ссылку в html-файле и попытался добавить текстовые/css mimetypes, но он продолжает выдавать ошибку.
Попробуйте изменить порядок установленных приложений следующим образом:
INSTALLED_APPS = [
"blog",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
Мое предположение, что django.contrib.contenttypes
не работает должным образом, потому что он указан перед приложением blog
, а не после него.
Вы забыли завернуть содержимое в блок. Но позвольте мне поделиться базовым примером, где вы можете выводить стили и скрипты на страницу, в дополнение к общим css и js файлам
base.html
{% load static %}
<!DOCTYPE html>
<html lang='en'>
<head>
<link rel="stylesheet" href="{% static 'base.css' %}">
{% block style %}{% endblock %}
<title>{% block title %}My amazing site{% endblock %}</title>
<meta charset='utf-8'>
</head>
<header>
<!-- Include a Navbar for instance -->
</header>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
<footer>
{% block script %}{% endblock %}
</footer>
</html>
extended_base.html
{% extends 'base.html' %}
{% load static %}
{% block style %}
<link rel="stylesheet" href="{% static 'base_extended.css' %}">
{% endblock %}
{% block content %}
<h1 class="from-extended-base"> My awesome Content</h1>
<h2 id="demo"> </h2>
{% endblock %}
{% block script %}
<script>
window.onload = function() {
console.log('hello!')
document.getElementById("demo").innerHTML = 'cool!';
}
</script>
{% endblock %}