Bitchdragon
Bitchdragon

Reputation: 105

Django doesn't see static css files

Faced a problem: django does not see static files. In particular css.

Here is the project structure:

enter image description here

settings.py (DEBUG = True):

STATIC_URL = '/static/'

STATICFILES_DIRS = []

STATIC_ROOT = os.path.join(BASE_DIR, "static")

aboutus.html

{% extends 'mainapp/base.html' %}
{% load static %}

<html>
    <head>
        <link type="text/css" href="{% static 'mainapp/css/aboutus.css' %}" rel="stylesheet" /> # здесь aboutus.css pycharm подчеркивает, ибо не видит
    </head>
    <body>
        {% block title %}
        О нас
        {% endblock %}
        {% block content %}

        <div id='div1'>
            <span id='span1'>▼</span> Кто мы такие?</div>
        <div id='div2'>
            1 <span class='span2'>2</span>3
            <span  class='span2'>4 </span><br>
            5 <br>
            6 <a href="https://www.youtube.com/watch?v=OWycy6WRv7w">7</a>
        </div>

        {% endblock %}
    </body>
</html>

aboutus.css :

#div1 {
 font-size: 20px;
 text-align: center;
 margin-top: 30px;
 margin-bottom : 20px
}
#span1 {
    font-size: 9pt
}
#div2 {
    box-shadow: 0 0 10px rgba(0,0,0,0.5);
    background-color: rgb(255,255,255);
    border-radius: 5px;
    margin-left: 10px;
    margin-right: 10px;
    line-height: 2.5;
    text-align: center;
    margin-bottom : 20px;
    border: 1px solid gray
}
.span2 {
    color: red
}

urls.py

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

'django.contrib.staticfiles' in INSTALLED_APPS is. I use mac m1.

UPD: I redid the design of the project according to your advice. Now it looks like this

enter image description here

SETTINGS.PY now looks like this:

STATIC_URL = '/static/'

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

STATIC_ROOT = os.path.join(BASE_DIR, "static")

Aboutus.html


{% extends 'mainapp/base.html' %}
{% load static %}

<html>
    <head>
          <meta charset="UTF-8">
          <link rel="stylesheet" href="{% static 'css/aboutus.css' %} /" type="text/css">
    </head>
    <body>
        {% block title %}
        О нас
        {% endblock %}
        {% block content %}

        <div id='div1'>
            <span id='span1'>▼</span> Кто мы такие?</div>
        <div id='div2'>
            1 <span class='span2'>2 </span>3
            <span  class='span2'>bitchdragon </span><br>
            4 <br>
            5 <a href="https://www.youtube.com/watch?v=OWycy6WRv7w">6</a>
        </div>
        {% endblock %}
    </body>
</html>

STATICFILES_FINDERS

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',

]

But still nothing helps

Upvotes: 0

Views: 788

Answers (1)

Patryk Szczepański
Patryk Szczepański

Reputation: 784

I see the comments but I can't answer in them, so I decided to help you via writing answer directly.

Basically what you did wrong is pasting static folder in wrong root directory - your root directory (BASE_DIR) is djangopractice1.

But, if you want to have static directories in the separate apps, you can always set STATICFILES_DIRS to this in settings:

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

Upvotes: 1

Related Questions