# Adding a tour to a page A tour is a short list of steps that Driver.js shows over one page, each step pointing at a part of the page with a title and a sentence. You declare the tour in Python, next to the view that renders the page. The page plays it by itself the first time a browser opens it. ## Before you start Include the package's stylesheet and scripts in your base template, once for the whole project. [Installation](installation.md#include-the-packages-files-in-your-base-template) shows the three lines. A page with a tour then needs two things: the view puts the tour in the template context under `tour`, and the page's template outputs it with `{{ tour }}`. ## Name the tour Every tour has a name, given where you declare it: ```python Tour(name="dashboard", steps=[...]) ``` The name is how the tour is known. A browser remembers that it has seen the tour by its name, and a project changes an installed app's tour by its name (see [Tours from reusable apps](tours-from-reusable-apps.md)). Pick one that stays put: - **Keep the name when you change the tour.** Renaming it makes it a different tour, so every visitor sees it again. To show a changed tour again on purpose, raise its version instead (see [Show a changed tour again](#show-a-changed-tour-again)). - **Give each tour its own name.** Two tours with one name share one seen state, so seeing one hides the other. Django's `check` command warns with `page_tours.W005` when tours with different steps are declared with the same name. One tour used on several views is still one tour, and so is one tour declared at two versions. - **In a reusable app, start the name with the app label:** `"."`, such as `"library.book"`. That keeps it apart from the tours of the project and of every other app. - **The name is in the page's source,** sent to the browser with the steps. Do not put anything in it you would not show a visitor. A name that is not a string with something in it raises `ImproperlyConfigured` when the tour is declared. ## Add a tour to a class-based view Put the tour in the view's context under `tour`. `extra_context` is the shortest way: ```python from django.views.generic import TemplateView from page_tours import Tour, TourStep DASHBOARD_TOUR = Tour( name="dashboard", steps=[ TourStep(title="Welcome", description="A quick look around."), TourStep( title="Your projects", description="Everything you work on is listed here.", element="#projects", ), ], ) class DashboardView(TemplateView): template_name = "dashboard.html" extra_context = {"tour": DASHBOARD_TOUR} ``` A view that builds its context in `get_context_data` adds the tour there instead: ```python class DashboardView(TemplateView): template_name = "dashboard.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["tour"] = DASHBOARD_TOUR return context ``` The tour is on every URL that routes to the view, and on every subclass that inherits the context. To put a tour on one URL only, give it through `as_view`: `DashboardView.as_view(extra_context={"tour": DASHBOARD_TOUR})`. ## Add a tour to a function-based view Pass the tour in the template context under `tour`, like any other value: ```python from django.shortcuts import render def dashboard(request): return render(request, "dashboard.html", {"tour": DASHBOARD_TOUR}) ``` Declare the tour at module level, as above, rather than inside the view. It is then checked when the module is imported, and the system checks know its name. ## Output the tour in the page's template Place `{{ tour }}` in the template of the page: ```django {% extends "base.html" %} {% block content %}

Dashboard

{{ tour }} {% endblock content %} ``` `{{ tour }}` outputs the tour as a `