Home > Blockchain >  Can't get Django Template Inheritance to work
Can't get Django Template Inheritance to work

Time:03-12

This seems so simple and yet it refuses to work...

I have my base.html file:

{% load static %}
<html>
<head>
  <meta charset="utf-8">
  <title>
    {% block title %}
    Simple Skeleton
    {% endblock title %}
  </title>

and my home.html file:

{% extends "base.html" %}
<% block title %>
Resource List
<% endblock %>

(they are both a bit longer but I feel this is all that is necessary)

The home.html completely fails to overwrite the sections within the code blocks - the page title remains "Simple Skeleton" and not "Resource List" despite me accessing the home.html file.

What am I doing wrong?

CodePudding user response:

Django template tags are written between {% … %}, not <% … %>, so {% block title %}, not <% block title %>:

{% extends "base.html" %}
{% block title %}
Resource List
{% endblock %}

By not using the proper template tag, Django will not render this content: it is content outside a block, so it will not have an effect on the inherited template.

CodePudding user response:

You have a typo error in home.html. Use {% %} for template tags instead of <% %>.

{% extends "base.html" %}
{% block title %} 
    Resource List
{% endblock %}
  • Related