martin
martin

Reputation: 885

Extending template using Django doesn't work

I'm having trouble displaying my content with Django in my ecommerce app. This is the base url with only the navigation bar:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="{% static 'css/style_store.css' %}">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <title>Inicio</title>
</head>
<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Mi ecommerce</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Inicio<span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
      </ul>
      <ul class="navbar-nav mr-auto">
        <form class="form-inline my-4">
          <input class="form-control mr-sm-2" type="search" placeholder="Buscá productos" aria-label="Search">
          <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
      </ul>
      <ul class="navbar-nav">
        {% if user.is_authenticated %}
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              {{ user }}
            </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                <a class="dropdown-item" href="#">Mi perfil</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" href="#">Ventas</a>
                <a class="dropdown-item" href="#">Compras</a>
              </div>
          </li>
        {% else %}
        <li class="nav-item">
          <a class="nav-link" href="{% url 'login' %}">Logueate</a>
        </li>
        {% endif %}
      </ul>
    </div>
  </nav>
  {% block content %}
  {% endblock content %}
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>

As you can see, I have the {% block content %} tag so I can extend this website with the homepage. This is home.html:

{% extends 'store/store.html' %}

<h1>HelloWorld</h1>

The issue is that I cannot see "HelloWorld", only the navigation bar from the base html. Any help will be appreciated!

Upvotes: 1

Views: 32

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32244

You need to put your content in a block that is defined in the template you are extending for it to render. In this case the content block

{% extends 'store/store.html' %}

{% block content %}
    <h1>HelloWorld</h1>
{% endblock content %}

The blocks in the "parent" template define the sections where you can insert content in an extending template, you need to use {% block %} in the extending template to tell the renderer which section you are placing content into. Anything outside of a {% block %} will not be rendered

Upvotes: 4

Related Questions