user7657641
user7657641

Reputation:

Django css static not load

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>

Upvotes: 1

Views: 572

Answers (2)

Zollie
Zollie

Reputation: 1211

Try this in settings.py also:

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

Upvotes: 0

Robert
Robert

Reputation: 3483

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.

Upvotes: 2

Related Questions