L
L
Lord_Dantes2019-04-12 09:56:36
Django
Lord_Dantes, 2019-04-12 09:56:36

How to include CSS (at least one file) in django?

I have already reviewed AND studied more than 10 articles. I read the documentation 3 times...
There is this: As I understand it, this is a relative path in django to my static files. Next, there is this:
STATIC_URL = '/static/'

INSTALLED_APPS = [
    'products',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Well, this is understandable according to the standard, I didn’t delete anything and didn’t invent it.
Next is this:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>sitename</title>
  <link rel="stylesheet" type="text/css" href="{% static 'main.css' %}">

</head>
<body>

  {% include 'navbar.html' %}

  {% block content %}

  replace me

  {% endblock %}

</body>
</html>

From above I connect statics, that's what I understand. Next, we <link>connect the static url again and it should find my main.CSS file.
But stupidly nothing works, the error is this.
5cb0363b13960734424287.png
The project structure is as follows: I
5cb03689ee1f2018481806.png:c
connect everything in a file base.html
Dear people, thank you very much for reading this nonsense about connecting a file to Django. Thanks for any help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
latush, 2019-04-12
@Lord_Dantes

The 'static' directory must be in the application's directory

L
lev_a_zhuravlev, 2021-04-22
@lev_a_zhuravlev

There was a similar error
, it was necessary to write before connecting the link {% load static %}
plus that the static was in the application directory

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question