Home > Back-end >  Css static in html use django, css no show out
Css static in html use django, css no show out

Time:11-30

main.html

with {% load static %} at top and {% static 'css/main.css' %} in link css href

{% load static %}

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CM1</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

    <link rel="stylesheet" type="test/css" href="{% static 'css/main.css' %}">
  </head>

  <body>
    {% include "accounts/navbar.html" %}

    {% block content %}

    {% endblock content %}

    <hr>
    <h5>Our footer</h5>

  </body>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL jjXkk Q2h455rYXK/7HAuoJl 0I4"
    crossorigin="anonymous"></script>

</html>

setting.py

import os


INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles', #<-- already have  
    ...
]

STATIC_URL = 'static/'

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

also STATICFILES_DIRS bracket should be () or []?

CodePudding user response:

You did wrong in link tag.

change this:

<link rel="stylesheet" type="test/css" href="{% static 'css/main.css' %}"> #You did wrong in type. and according to your structure your css filename is index.css

To this:

<link rel="stylesheet" type="text/css" href="{% static 'css/index.css' %}"> #changed main.css to index.css according to your structure

Your settings must be like below.

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
  • Related