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>
2
  • 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. Commented Aug 25, 2017 at 11:55
  • Possible duplicate of Django: CSS Is not not working Commented Aug 25, 2017 at 12:06

2 Answers 2

2

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.

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

Comments

0

Try this in settings.py also:

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

Comments

Your Answer

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