Menu
CoddyTech

Including a Template

Part of the Django Basics section of Coddy's Django journey. Lesson 19 of 23.

A drink card (name, description, price) belongs on the home page for today's special and on /drinks/ for every drink. It is ready in menu/templates/menu/drink_card.html. Instead of copying it into two templates, include it:

{% include "shop/price_tag.html" %}

The included template is pasted in where the tag is, and it sees the same variables. If the price tag reads {{ product.price }}, it works inside {% for product in products %}. Where the item has another name, with passes the name the piece expects:

{% include "shop/price_tag.html" with product=deal %}
{% extends %} shares a frame; {% include %} shares a piece. Change the piece once and every page follows.
challenge icon

Challenge

Easy
  • On the home page, replace <p>Today's special: ...</p> with <p>Today's special:</p> followed by the drink card for special.
  • On /drinks/, inside each <li class="drink">, replace the link and the price with the drink card. Keep the Sold out mark.

Press Run: both pages show the same cards.

Try it yourself

{% extends "menu/base.html" %}

{% block content %}
    <h1>Welcome to Corner Café</h1>
    <p>Fresh coffee and cake every day.</p>
    <p>Today's special: {{ special.name }}</p>
    <a href="{% url 'menu:about' %}">About us</a>
{% endblock %}

All lessons in Django Basics

Practice on your own: Django playground