HTML not loading CSS in Django

Viewed 213

I have a problem that my HTML code does not load my css files. The code works if put into the main.html tag, however as outsourced to the separate css files, not working at all.

Link to the project: https://github.com/Svantevith/Python/tree/master/ToDoList

The overall structure of the folder: enter image description here

The head tag of main.html:

<head>
    <title>ToDoList App</title>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> -->
    <link id="sheet-theme" rel="stylesheet" type="text/css" href="light.css"> 

    <!-- Nunito Font -->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@200&display=swap" rel="stylesheet">
</head>

In my django project, the rest of HTML files extend the main.html file, for example the register view:

{% extends 'base/main.html' %}

{% block content %}

    <div class="header-bar">
        <h1>Register</h1>
    </div>

    <div class="card-body">
        <form method="POST">
            {% csrf_token %}
            {{form.as_p}}
            
            <input style="margin-top: 16px" class="button" type="submit" value="Register">
        </form>

        <p>Already have an account? <a id="plain" href="{% url 'login' %}">Login</a></p>
    </div>

{% endblock content %}

What might be the problem?

EDIT: Solved the problem by adding the base/static/base folder and accessing the css via {% static 'base/file.css' %} as mentioned in the comments and official documentation ;)

2 Answers

You have to just put the {% load static %} on top of your main.html code

And in the Head section, you should change the link as I did in href.

<link id="sheet-theme" rel="stylesheet" type="text/css" href="{% static 'light.css'%}">

Your href="light.css" is the problem here. The system is looking for that file in the main folder that you are in. You have to tell the system to go to the base folder in the template folder using Jinja syntax. Your link should be

<link id="sheet-theme" rel="stylesheet" type="text/css" href="{% static light.css %}"> 
Related