Home > front end >  How to connect my HTML file with CSS file inside of a Django Project
How to connect my HTML file with CSS file inside of a Django Project

Time:12-21

I have a simple html page that works and renders properly on my local browser, but when I reference the static css file the page loads without the styling and I get a 200 Success for the url but 404 for the style.css file

I used

 <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">

inside of the HTML file. I have the static folder in the correct spot at the project level and then a css file inside that followed by the style.css file.

The Html page:

{% load static %}
<!DOCTYPE html>
<html>

<head>
    <title>My Website</title>
    <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
</head>

<body>
    <h1>Home</h1>
    <header>
        <nav>
            <ul>
                <li><a href="{% url 'home' %}">Home</a></li>
                <li><a href="{% url 'about' %}">About</a></li>
                <li><a href="{% url 'info' %}">Info</a></li>
            </ul>
        </nav>
    </header>
</body>

</html>

The CSS page:

h1 {
    background-color: orange;
}

From the research I've done this should get the backgrounds of all the h1 tags orange but it is not working. Any Advice?

CodePudding user response:

handle Static Files In dajngo

Steps:

# 1 - Create (static) folder in Root Directory of Project
# 2 - Paste Your Static Files Folder in (static) Folder (js,css,images...etc)
# 3 - Now Do Setting in (setting.py)
       STATIC_URL = '/static/'

       STATICFILES_DIRS = [os.path.join(BASE_DIR,'static') ]

       ---------- OR -------- 

       STATICFILES_DIRS = [BASE_DIR / 'static']


# 4 - add {% load static %} tag on top of html page
# 5 - Now use ({% static 'assets/image.jpg' %}) tag in HTML File for calling static files
# 6 - Done

CodePudding user response:

Thanks to the answers I was able to get an orange background for all the h1 tags. I added STATICFILES_DIRS = [BASE_DIR / "static"] to the settings.py file and it worked perfectly!

  • Related