Caching dei frammenti di template
Mettere in cache solo le parti costose di una pagina
Caching dei frammenti di template è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Cache Just One Piece
Often only part of a page is slow. Fragment caching lets you cache that one block while the rest renders fresh each time.
Load the cache Tag
Fragment caching lives in a template tag library, so start your template with load cache to make it available.
{% load cache %}The cache Block
Wrap the slow markup in a cache block. Django renders it once, stores the HTML, and reuses it on later requests.
{% cache 500 sidebar %}
...expensive sidebar...
{% endcache %}Timeout Comes First
The first argument is the timeout in seconds. After it expires, the next render rebuilds the fragment and caches it again.
Name Your Fragment
The second argument is a fragment name. Django combines it with extra arguments to form the unique cache key.
Vary the Fragment
Add variables after the name so a fragment can vary per user or object, caching one copy for each distinct value.
{% cache 500 sidebar request.user.id %}
...per-user sidebar...
{% endcache %}Each Variation Is Separate
Every combination of the vary arguments gets its own key, so user 1 and user 2 each see their own cached copy.
Use a Specific Cache
Point a fragment at a named cache with using when you keep page fragments separate from other cached data.
{% cache 500 sidebar using="fragments" %}Great for Shared Widgets
Fragment caching shines for shared widgets like a navbar or a top-posts list that look the same for many visitors.
Keep Live Parts Outside
Leave anything that must stay current, like a greeting with the user's name, outside the cache block so it never goes stale.
Less Effort Than View Caching
Fragment caching is the middle ground: more precise than caching a whole view, yet far simpler than caching every query by hand.
Quick Check
Let's check how a fragment stays unique per user.
Recap
You can now load cache, wrap a fragment in a cache block with a timeout and name, and vary it per user for precise speedups. ✨
Domande Frequenti
La lezione «Caching dei frammenti di template» è gratuita?
Sì — il testo completo di «Caching dei frammenti di template» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.
Cosa imparerò in «Caching dei frammenti di template»?
Mettere in cache solo le parti costose di una pagina Eserciti Django Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Django Academy?
Non è richiesta alcuna esperienza precedente. Django Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Caching dei frammenti di template»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Django Academy?
Sì. Ogni lezione Django Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Backend di cache e Redis
- Caching per vista e per sito
- Caching dei frammenti di template
- API cache di basso livello e invalidazione