0

my first question here. I'm still new to both Django and CSS.

I'm trying to use CSS from Static folder in my Django project. It's all at a pretty basic stage.

In the 'static' subfolder 'css' I have one main.css file with example code:

body{
    font-size: 20px;
    background-color: black;
}

h1{
    color: #008000;
}

In my settings.py file I have:

import os
(...)
DEBUG = True
(...)
STATIC_URL = '/static/'

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

And in my base.html template:

{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shopping List</title>
    <link rel="stylesheet" type="text/css" href="{% static '/css/main.css' %}">
</head>
<body>
<h1>TEST</h1>
{% block content %}
{% endblock %}
</body>
</html>

I've tried moving the css file, changing the view/url, and adding/deleting bootstrap (which alone works).

In the end my page just turns light blue-ish.

Thanks in advance.

5
  • what help you want from us? Commented Nov 23, 2020 at 18:27
  • is your css file is not loading? Commented Nov 23, 2020 at 18:28
  • Yes, my css file is not working. Commented Nov 23, 2020 at 18:39
  • Yes, exactly, it does. I just posted it 10 minutes ago but it was deleted by the moderator. Commented Nov 24, 2020 at 17:40
  • That was because your answer was link-only... If you feel like your question is a duplicate of that question then please close-vote it as a duplicate instead of writing a link-only answer. Commented Nov 24, 2020 at 17:43

2 Answers 2

1

So I checked your code and for me it works fine check if you have 'django.contrib.staticfiles' in your INSTALLED_APPS

If this didn't work I'm going to leave my code here, hope it helps.

-DjangoProject
    -static
       -css
         -testing.css

template.html

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

settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',]

...
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

I just noticed that my STATICFILES_DIRS is a tuple and not a list like in your example, I don't think it makes any difference but you could try it.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately that didn't help. I might try doing everything from scratch once again or using different environment.
0

Not sure if this will work, but try removing the "/" before css in your html file:

... href="{% static 'css/main.css' %}">

1 Comment

Nope, that's not it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.