0Pricing
Flask Academy · Lekcja

Komunikaty flash między żądaniami

Wyświetlaj jednorazowe powiadomienia po przekierowaniu.

Komunikaty flash między żądaniami to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 4 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 Flask Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Flask Academy zawiera 4 lekcji w sumie.

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

The One-Time Notice

After saving a form you often want to show Saved! once, then never again. Flask calls this a flash message. ✨

Why Not Just Pass It

The trick is that you usually redirect after a save, and the redirect loses your variables. Flash survives that hop for you.

It Rides the Session

Under the hood, flash tucks the message into the session, so you must have a SECRET_KEY configured for it to work.

Flash a Message

In your view, call flash with the text right before you redirect the user to the next page.

from flask import flash, redirect
flash('Profile saved!')
return redirect('/profile')

Read Them in the Template

On the destination page you pull the queued notices with get_flashed_messages and loop over them in Jinja2.

{% for m in get_flashed_messages() %}
  <p>{{ m }}</p>
{% endfor %}

Shown Once Only

Reading the messages also clears them. Refresh the page and the notice is gone, exactly the behavior you want.

Add a Category

Pass a second argument to tag a message with a category like success or error, so you can style each kind differently.

flash('Wrong password', 'error')

Read Categories Too

Ask for categories with with_categories set to true, and each entry becomes a category and message pair.

{% for cat, m in get_flashed_messages(with_categories=true) %}

Filter by Category

You can also request only certain kinds using category_filter, handy when one block shows just errors.

get_flashed_messages(category_filter=['error'])

Put It in the Layout

Render flashes in your base template so every page can surface notices without repeating the loop everywhere.

Keep Them Short

Flash is for brief, friendly feedback, not long reports. One clear sentence per message keeps the experience clean.

Quick Check

Consider what happens after a template reads the flashed messages.

Recap

You learned that flash queues a one-time message through the session, survives a redirect, and clears once shown. 🎉

Często zadawane pytania

Czy lekcja „Komunikaty flash między żądaniami” jest bezpłatna?

Tak — pełny tekst „Komunikaty flash między żądaniami” 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 Flask Academy, przejdź na CoddyKit PRO. Kurs Flask Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Komunikaty flash między żądaniami”?

Wyświetlaj jednorazowe powiadomienia po przekierowaniu. Ćwiczysz Flask 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ąć Flask Academy?

Nie wymagamy żadnego doświadczenia. Flask 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 4 z 4.

Ile czasu zajmuje lekcja „Komunikaty flash między żądaniami”?

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 Flask Academy?

Tak. Każda lekcja Flask 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. Ustawianie i odczytywanie słownika session
  2. SECRET_KEY i podpisane pliki cookie
  3. Ustawianie własnych plików cookie w odpowiedzi
  4. Komunikaty flash między żądaniami
← Powrót do Flask Academy