Flask Academy · درس

رسائل Flash بين الطلبات

اعرض إشعارات لمرة واحدة بعد إعادة التوجيه.

الدرس 4 من 413 خطوة

رسائل Flash بين الطلبات درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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. 🎉

البدء مجانًا

تعلم Python مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «رسائل Flash بين الطلبات» مجاني؟

نعم — نص درس «رسائل Flash بين الطلبات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «رسائل Flash بين الطلبات»؟

اعرض إشعارات لمرة واحدة بعد إعادة التوجيه. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «رسائل Flash بين الطلبات»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ضبط قاموس session وقراءته
  2. SECRET_KEY وملفات تعريف الارتباط الموقّعة
  3. ضبط ملفات تعريف ارتباط مخصصة في استجابة
  4. رسائل Flash بين الطلبات
← العودة إلى Flask Academy