1

I have a problem that my .css file doesn't apply on my .html file in Django. But when I, for example, type . some class name from the .html file, pycharm offers me to finish the class name. So, I think its linked right.

Anyway, I put in my settings file:

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

Run python manage.py collectstatic succesfuly and added my .css file to .html with command:

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

Inside of my project I have something like this:

-website
   -static
        -admin (generated when I run the command above)
        -images
        -base.css
    -templates
        -navbar.html
        -base.html
     -app1
     -app2
     -etc

For testing purpose I just put simple div in the head section of the .html file:

 <div class="example">jvhgbhjgf</div>

And in .css file:

.example { background-color: red; border-radius: 15px;}

2 Answers 2

2

Change your settings.py file like following:

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

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

and directory's tree:

-website
-static
    -static_root
        -admin (generated when I run the command above)
    -static_dirs
        -images
        -base.css
-templates
    -navbar.html
    -base.html
 -app1
 -app2
 -etc

add base.css file in static_dirs folder.

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

Comments

1

Add following code in your urls.py file:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns [ ... ]

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

9 Comments

It's the same, nothing happened. :(
After I did what you wrote in your previous response I can access, but, still, it wasn't applied on .html file.
I succeed after restarting server! Thank you, you made my day! :)
I just have one more question.. Now inside every app, I should create directories: static/static_dirs/name_of_app/my css files and inside the .html file link it with: {% load staticfiles %} <link rel="stylesheet" type="text/css" href="{% static 'name_of_app/style.css' %}"/>
I am asking this, because this method isn't working for me in my apps
|

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.