I
I
IVBAN2018-09-12 11:00:35
Django
IVBAN, 2018-09-12 11:00:35

Why, when inheriting a template, it is necessary to render not the base template, but the child one? Where is the logic here?

basic index.html template:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>{{ question }}</title>
</head>
<body>
  <h1>Привет</h1>

  <a href="#" id="mybtn">кнопка</a>
  <h2 id="l1">fffffff</h2>

  {% block question %}
    qwerqwer
  {% endblock %}

  <img src="{% static 'img1.jpg' %}" alt="">

  <script src="{% static 'js/jquery-3.3.1.min.js' %}"></script>
  <script src="{% static 'scripttest.js' %}"></script>
</body>
</html>

The template inherits the base template:
{% extends 'mainapp/index.html' %}

{% block title %}question{% endblock %}

{% block question %}
  <h2>1</h2>
  <h2>2</h2>
  <h2>3</h2>
{% endblock %}

and here is the view function:
def index(request):
    return render(request, 'mainapp/one_question.html', {'question': 'Привет'})

That's why the view function above doesn't need to be written like this:
def index(request):
    return render(request, 'mainapp/index.html', {'question': 'Привет'})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question