Why css don't loaded in django?

Issue

I encountered a problem. Just my .html, the css files not loaded,even I write the absolute path which can be visited from the browser as follows:

<link ref="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}" />
<link ref="stylesheet" type="text/css" href="http://127.0.0.1:8000/static/css/bootstrap.min.css" />

Could anyone please help me?

My PyCharm project location: E:\workspaces\workspace-py\python-test
django project relative lication: \django\djangotest

\template\base.html:

<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
    <meta charset="UTF-8">
    <title></title>
    <link ref="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}" />
    <link ref="stylesheet" type="text/css" href="{% static 'css/index.css' %}"/>

\static\css\bootstrap.min.css

\djangotest\settings.py

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

Solution

I’ve worked out after saw the source code of the "admin" app.
The tag of "block stylesheet" is needed.

    {% block stylesheet %}
    <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}">
    <link ref="stylesheet" type="text/css" href="{% static 'css/index.css' %}"/>
    {% endblock %}

Answered By – chris wae

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published