L
L
LenTop2020-11-04 18:57:58
Django
LenTop, 2020-11-04 18:57:58

Tags not working in Django template?

Hello! I have a problem using tags in Django since I'm new to programming. Here's the bottom line: when using {% extends %}, my html file doesn't inherit the parent's class. Also, the objects method in views.py does not work.
Version - Python 3.8
Django - 3.1.3
And also when the code is displayed in the browser, the tags themselves are displayed:
{% block title %}{% endblock %} {% load static %}
{% block sidebar %}
Home page
All books
All authors
{% endblock %}

This is views.py

from django.shortcuts import render
from .models import Book, Author, BookInstance, Genre


def index(request):
    """
    Функция отображения для домашней страницы сайта.
    """
    # Генерация "количеств" некоторых главных объектов

    num_books = Book.objects.all().count()
    num_instances = BookInstance.objects.all().count()
    # Доступные книги (статус = 'a')
    num_instances_available = BookInstance.objects.filter(status__exact='a').count()
    num_authors = Author.objects.count()  # Метод 'all()' применен по умолчанию.

    # Отрисовка HTML-шаблона index.html с данными внутри
    # переменной контекста context
    return render(
        request,
        'index.html',
        context={'num_books': num_books, 'num_instances': num_instances,
                 'num_instances_available': num_instances_available, 'num_authors': num_authors}
    )


This is the parent html - base_generic.html
<!DOCTYPE html>
<html lang="en">
<head>

    {% block title %}<title>Local library</title>{% endblock %}
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <!-- Добавление дополнительного статического CSS файла -->
    {% load static %}
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>

<body>

    <div class="container-fluid">

        <div class="row">
            <div class="col-sm-2">
                {% block sidebar %}
                <ul class="side-bar-nav">
                    <li><a href="{% url 'index' %}">Home page</a></li>
                    <li><a href="">All books</a></li>
                    <li><a href="">All authors</a></li>
                </ul>
                {% endblock %}
            </div>
            <div class="col-sm-10">
                {% block content %}{% endblock %}
            </div>
        </div>

    </div>

</body>
</html>


And child html - index.html
{% extends "templates/base_generic.html" %}

{% block content %}
<h1>Local Library Home</h1>

  <p>Welcome to <em>LocalLibrary</em>, a very basic Django website developed as a tutorial example on the Mozilla Developer Network.</p>

<h2>Dynamic content</h2>

  <p>The library has the following record counts:</p>
  <ul>
      <li><strong>Books:</strong> {{ num_books }}</li>
      <li><strong>Copies:</strong> {{ num_instances }}</li>
      <li><strong>Copies available:</strong> {{ num_instances_available }}</li>
      <li><strong>Authors:</strong> {{ num_authors }}</li>
  </ul>
{% endblock %}


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Black8, 2020-11-06
@Black8

Hello.
The error most likely lies in the line: " {% extends "templates/base_generic.html" %}" . (child html)
Try rewriting the tag in it to "{% extends "base_generic.html" %}". I hope it helps)) I'm learning Django myself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question