I can not extend the django template

I can not expand the template template base.html header.html

Content base.html

<div id="main-container">
  <!-- HEADER -->
  {% block header %}{% endblock %}
  <!-- END HEADER -->
</div>

Content header.html

{% extends "blog/base.html" %}
{% block header %}
<header id="header">
***
</header>
{% endblock %}

The result in the browser receives the code:

<div id="main-container">
  <!-- HEADER -->

  <!-- END HEADER -->

Why not expand the template? With the entered code {% include "blog/header.html"%}. using extendsno. Using Django 1.10.1

views.py

from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404

def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/index.html', {'posts': posts})

def post_detail(request, pk):
        post = get_object_or_404 (Post, pk=pk)
        return render(request, 'blog/base.html', {'post': post})

def header(request):
    return render(request, 'blog/header.html')

Through {% include "blog/header.html" %}works. So, the path is correctly formulated.

An error occurred here:

def header(request): return(request, 'blog/header.html')

def header(request): render(request, 'blog/header.html')

def header(request): return render_to_response (request, 'blog/header.html')

Does not work (((

+4
source share
2 answers

I think you would be confused between include and extension in django templates.

Based on your file names, I assume that header.htmlis the part that should be included in base.html, and you are rendering base.html.

The Django template engine does not work this way.

include {% include "path/to/header.html" %} base.html, juse - html header.html.

0

- , {% extends...%} ( header.html), - , {% include... %} . ovreload {% block head%}, :

base.html:

{% block header %}
{% include 'std_header.html' %}
{% endblock %}

{% block content %}
{% endblock %}

{% block footer%}
{% endblock %}

, , :

landing.html:

{% extends 'base.html' %}

{% block header %}
{% include 'landing_header.html' %}
{% endblock %}

{% block content %}
<!-- landing page content -->
{% endblock %}

, landing_page landing.html.

0

Source: https://habr.com/ru/post/1654244/


All Articles