[英]how to load css file in html file already extends another html file (base.html) in django
I'm new to django, so i get difficulty in dealing with django template in relation to adding css file into django template.My code is as follows:我是 django 的新手,所以在处理 django 模板时遇到了困难,因为我将 css 文件添加到 Z2B9AFB89A6ACC1575B159CA 如下:810BFADZ模板
base.html底座.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{% static 'portfolio/main.css' %}"> {% if title%}
<title>Django Portfolio - {{title}}</title>
{% else %}
<title>Django Portfolio</title>
{% endif %}
</head>
<body>
<div class="topnav">
<a class="active" href="{% url 'portfolio-home' %}">Home</a>
<a href="{% url 'portfolio-page' %}">Portfolio</a>
<a href="{% url 'portfolio-about' %}">About</a>
<a href="#blog">Blog</a>
<a href="#account">Account</a>
</div>
<div class="container">
{% block content %} {% endblock content %}
</div>
</body>
</html>
This is my base.html file in which all my other html files inherit it.It works perfectly fine.Note:-I create the static file inside django app and put two static files; This is my base.html file in which all my other html files inherit it.It works perfectly fine.Note:-I create the static file inside django app and put two static files; main.css and home.css for now.
main.css 和 home.css 现在。 But i want to design homepage, and i av done this with the file known as home.html and use the home.css in static file.My code is as follows:
但我想设计主页,我使用名为 home.html 的文件完成此操作,并在 static 文件中使用 home.css。我的代码如下:
home.html主页.html
{% extends 'portfolio/base.html' %}
{% load static %}
{% block content %}
<link rel="stylesheet" type="text/css" href=" {% static 'portfolio/home.css' %}" />
<h1>My homepage</h1>
{% endblock content %}
The problem is, the line <link rel="stylesheet" type="text/css" href=" {% static 'portfolio/home.css' %}" />
does not work.问题是,行
<link rel="stylesheet" type="text/css" href=" {% static 'portfolio/home.css' %}" />
不起作用。 I need help.我需要帮助。
<link rel="stylesheet" type="text/css" href="{% static 'portfolio/home.css' %}">
This should work, it's syntactically correct.这应该有效,它在语法上是正确的。 Are you sure you have your.css file in a folder structure like so: "/APPNAME/static/Portfolio/home.css".
你确定你的 .css 文件在这样的文件夹结构中:“/APPNAME/static/Portfolio/home.css”。
As others have mentioned, you are not including your CSS in the head of the HTML document, it is being placed into the body, inside the {% block content %}.正如其他人所提到的,您没有将 CSS 包含在 HTML 文档的头部,它被放入正文中,在 {% block content %} 内。
You need to wrap it in a {% BLOCK CSS %} {% ENDBLOCK %}您需要将其包装在 {% BLOCK CSS %} {% ENDBLOCK %}
{% extends 'portfolio/base.html' %}
{% load static %}
{% block css %}
<link rel="stylesheet" type="text/css" href="{% static 'portfolio/home.css' %}">
{% endblock %}
{% block content %}
etc...
It worked for me:它对我有用:
{% extends 'menu.html' %}
{% load static %}
<body>
{% block content %}
{% block css %}
<link rel="stylesheet" type="text/css" href="{% static 'moduleone.css' %}">
{% endblock %}
<div class="jumbotron">Test</div>
{% endblock %}
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.