0Pricing
Django Academy · Lekcja

TemplateView i get_context_data

Renderować szablon i przejrzyście dodawać kontekst

TemplateView i get_context_data to bezpłatna lekcja Django Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Django Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Django Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Meet TemplateView

TemplateView is a generic CBV whose only job is to render a template. It is the simplest class-based view you will use.

Set template_name

You tell TemplateView which file to render with the template_name attribute. No get method needed for a plain page.

class AboutView(TemplateView):
    template_name = "about.html"

Wire It Into urls

Add it to urls with as_view(), just like any CBV. That one line gives you a fully working page.

path("about/", AboutView.as_view())

Passing Data to Templates

A static page is fine, but usually you need context: data your template can display. CBVs provide a clean hook for that.

Override get_context_data

The get_context_data method builds the dictionary passed to your template. Override it to add your own values.

Always Call super() First

Start by calling super() to get the default context, then add to it. Skipping this can drop data Django expects.

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    return context

Add Your Own Keys

After super(), insert any keys you need into the context dict. The template reads them by name.

context["year"] = 2024
return context

Templates Read Context Keys

In the template, reference a context key with double braces. Whatever you put under year appears where you write it.

kwargs Carries URL Data

The kwargs argument holds values captured from the URL, like an id. You can read them while building context.

Less Boilerplate Than Functions

Compared to a function view, TemplateView removes the repetitive render call. You declare the template once and add only the data.

A Common First CBV

TemplateView plus get_context_data is the pattern you will reach for whenever a page needs a template and a bit of dynamic data.

Quick Check

Which method do you override to add data for a TemplateView?

Recap: TemplateView

You learned TemplateView: set template_name to render a page, and override get_context_data, calling super first, to pass data along. ✨

Często zadawane pytania

Czy lekcja „TemplateView i get_context_data” jest bezpłatna?

Tak — pełny tekst „TemplateView i get_context_data” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Django Academy, przejdź na CoddyKit PRO. Kurs Django Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „TemplateView i get_context_data”?

Renderować szablon i przejrzyście dodawać kontekst Ćwiczysz Django Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Django Academy?

Nie wymagamy żadnego doświadczenia. Django Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „TemplateView i get_context_data”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Django Academy?

Tak. Każda lekcja Django Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Od widoków funkcyjnych do klas widoków
  2. TemplateView i get_context_data
  3. Obsługa metod get i post
  4. Mixiny i as_view()
← Powrót do Django Academy