1

CSS not load!! I have read some similar questions but I can't solve this problem. Why I wrong?

Static directory path:

/project/static

settings.py

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

base.html

<!DOCTYPE html>
{% load static %}

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">
<title>title</title>
</head>
<body
{% block content %}
{% endblock content %}
</body>
</html>
  • Can you check your console to see if you're getting any network errors? Specifically a 404, and that will give you an idea of what URL it's trying to load the CSS from. You can also check what's been rendered on the page to see if it's what you're expecting. – wholevinski Aug 25 '17 at 11:55
  • Possible duplicate of [Django: CSS Is not not working](https://stackoverflow.com/questions/13446325/django-css-is-not-not-working) – Brown Bear Aug 25 '17 at 12:06

2 Answers2

1

This as per document,you can try like this

settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',# Here you can mention your static directory
]

urls.py

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


urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

in your template

{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image"/>

Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg.

Robert
  • 3,373
  • 1
  • 18
  • 34
0

Try this in settings.py also:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
Zollie
  • 1,171
  • 7
  • 14