0

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

8
  • move css folder to static, mainapp is not necessary, must be /static/css/startpage.css Commented Jul 21, 2022 at 16:25
  • it didnt help me Commented Jul 21, 2022 at 16:30
  • My app has a similar folder structure as yours, but I utilize backslashes <link rel="stylesheet" type="text/css" href="{% static 'mainapp\css\startpage.css' %}"/> Commented Jul 21, 2022 at 19:43
  • It doesnt work too Commented Jul 21, 2022 at 20:45
  • 1
    STATIC_URL is pointing to the base directory of /static/ but that does not match your directory structure. I actually prefer to build a static folder in the project root directory for a number of reasons, and it works very well in both dev and prod environments. Create a static folder in the project root directory, copy all your css files there (only delete original css location new path is working). Commented Jul 21, 2022 at 20:51

1 Answer 1

1

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'),
]
Sign up to request clarification or add additional context in comments.

6 Comments

I set static to both djangopractice1 and Practice1 but nothing works. Project root directory - practice1
Try moving static dir to any app folder and define STATICFILES_DIRS.
It doesnt help too
Change <link rel="stylesheet" href="{% static 'css/aboutus.css' %} /" type="text/css"> to <link rel="stylesheet" href="{% static 'css/aboutus.css' %}" type="text/css"> - I noticed this in this moment - and it should work.
But you gave exactly the same link? I checked. still not working(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.