Y
Y
Yoshinon Eared2017-07-13 13:19:10
Django
Yoshinon Eared, 2017-07-13 13:19:10

Django templates or how to understand them?

Good day!
I'm learning templating in django. There were some problems with understanding their work.
For example, there is a skeleton template base.tpl :

<html>
  <head>
    <title>{% block title %}Родительский заголовок{% endblock %}</title>
  </head>
  <body>
    {% block header %}{% endblock %}
    {% block content %}{% endblock %}
    {% block footer %}{% endblock %}
  </body>
</html>

...and the corresponding title.tpl , header.tpl , content.tpl , footer.tpl , their contents are something like this (using title.tpl as an example ):
{% extends "template/base.tpl" %}
{% block title %} Дочерний заголовок {% endblock %}

, i.e. I override the block specified in base.tpl in title.tpl .
And also views.py :
Option 1:
def index(request):
  return render(request, 'template/base.tpl');

In the title we get "Parent title".
Option 2:
def index(request):
  return render(request, 'template/title.tpl');

In the header we get "Child header".
Everything is clear, everything is clear.
And then I start to get lost.
What about other child templates like header.tpl , content.tpl , footer.tpl ?
I have a strong suspicion that I just misunderstood the very essence of templates and distorted its concept.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-07-13
@Aquinary

misunderstood the essence of templates

def page_about(request):
  return render(request, 'about.html');

about.html
{% extends "base.html" %}
    {% block header %} Here is the title of about page{% endblock %}
    {% block content %}content about page{% endblock %}
    {% block footer %}footer for about page{% endblock %}

What looks like include for you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question