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:
base.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; main.css and home.css for now. 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
{% 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. I need help.
<link>with rel attribute "stylesheet" is body-ok, so it may appear in the body, however I'm not 100% sure this is accepted by all browsers, as it's very uncommon practice. Which browser are you testing with?{% block extra_css %} {% endblock %}in your base template and paste your code in the same blocks{% block extra_css %} <link rel="stylesheet" type="text/css" href=" {% static 'portfolio/home.css' %}" /> {% endblock %}in the home.html template filerelset to"stylesheet"is body-ok. You're referring to HTML4 spec, but we live in HTML5 times. I've tested in Safari and Chrome and <link> tags for css inside the <body> work. Again, I'm not sure it works in all browsers and I agree it's better to put it in <head> but I'm not sure this is the reason for OP's problem.