Answer the question
In order to leave comments, you need to log in
Styles are not applied in Django. What's wrong?
I created a second application in Django, and in it I created an HTML document (news.html)
{% extends 'index/layout.html' %}
{% block title %}Новости{% endblock %}
{% block content %}
<h1>Новости</h1>
{% for el in news %}
<div class="alert alert-warning">
<h3>{{ el.title }}</h3>
<p>{{ el.anons }}</p>
</div>
{% endfor %}
{% endblock %}
{% load static %}
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href=" {% static 'index/css/layout.css' %} ">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
<link rel="stylesheet" href="{% static 'news/css/news.css' %}">
</head>
<body>
<aside class="back">
<img src="{% static 'index/img/logo1.png' %}" alt="Логотип">
<span class="logo">Django</span>
<h3 class="navigation">Навигация</h3>
<ul>
<a href="{% url 'index' %}"><li><i class="fas fa-home"></i> Главная</li></a>
<a href="{% url 'about' %}"><li><i class="fas fa-address-card"></i> Про нас</li></a>
<a href="{% url 'contacts' %}"><li><i class="fas fa-address-book"></i> Контакты</li></a>
<a href="{% url 'news' %}"><li><i class="fas fa-newspaper"></i> Новости</li></a>
</ul>
</aside>
<main>
{% block content %}
{% endblock %}
</main>
</body>
</html>
from django.urls import path
from . import views
urlpatterns = [
path('', views.news, name='news'),
]
from django.shortcuts import render
from .models import MyModel
def news(request):
news = MyModel.objects.all()
return render(request, 'news/news.html', {'news': news})
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('index.urls')),
path('news/', include('news.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('contacts/', views.contacts, name='contacts')
]
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'index/index.html')
def about(request):
return render(request, 'index/about.html')
def contacts(request):
return render(request, 'index/contacts.html')
Answer the question
In order to leave comments, you need to log in
1. Modify the base template by adding an empty block in the head after including all the CSS:
2. In the extension template, fill in this block:
{% block css %} {% endblock %}
{% block css %}
<link rel="stylesheet" href="{% static 'path/to/css.css' %}">
{% endblock %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question