Menu
CoddyTech

Site Navigation

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

The site has five pages with the shared layout now (About got a template too), but the nav in base.html lists three, and it never shows where you are.

Every template can read request, the request being answered, because settings.py lists the request context processor in TEMPLATES. request.resolver_match describes the route that matched: url_name is its name without the namespace ("contact"), and view_name the full name ("menu:contact"). A frame can show a line on one page only:

{% if request.resolver_match.url_name == "contact" %}
    <p>We reply within a day.</p>
{% endif %}
challenge icon

Challenge

Medium

In base.html, make the nav link Home, Menu, Drinks, Hours and About, in that order, each address built with {% url %}. On each page, only the link to that page gets class="active".

Give nav a.active a style in style.css, like bold text, then press Run and click through the nav.

Try it yourself

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{% block title %}Corner Café{% endblock %}</title>
    <link rel="stylesheet" href="{% static 'menu/style.css' %}">
</head>
<body>
    <nav>
        <a href="{% url 'menu:home' %}">Home</a>
        <a href="{% url 'menu:drinks' %}">Drinks</a>
        <a href="{% url 'menu:about' %}">About</a>
    </nav>
    <main>
        {% block content %}{% endblock %}
    </main>
</body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Django Basics

Practice on your own: Django playground