0Pricing
Flask Academy · درس

إعادة التوجيه بعد POST (نمط PRG)

تجنّب الإرسال المكرر باستخدام إعادة التوجيه.

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

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

The Double Submit Problem

After a form post, hitting refresh resends the same data. That can create duplicate orders, comments, or signups. 😬

Why Refresh Repeats

The browser remembers the last request. Reloading a page that was the result of a POST simply fires that POST again.

Meet Post Redirect Get

The fix is the PRG pattern: accept the POST, then redirect the browser to a plain GET page instead of rendering directly.

Process the POST

First do the real work inside the POST branch: save the record, charge the card, whatever the form was for.

if request.method == "POST":
    save_comment(request.form["text"])

Redirect, Do Not Render

Instead of returning a template, return a redirect. The browser then makes a fresh GET that is safe to refresh.

from flask import redirect
return redirect("/thanks")

Build the Target With url_for

Avoid hardcoded paths. Use url_for so the redirect target follows your route names even if URLs change later.

return redirect(url_for("thanks"))

The 302 Status

A redirect sends a 302 response telling the browser to go fetch another URL. That second request is the safe GET.

The GET Handler

The target route is an ordinary GET view. It just shows a confirmation page and can be refreshed all day with no side effects.

@app.route("/thanks")
def thanks():
    return render_template("thanks.html")

Carry a Flash Message

Want to say it worked? Set a one time flash message before redirecting and show it on the destination page.

from flask import flash
flash("Saved!")
return redirect(url_for("thanks"))

Redirect on Errors Too

If validation fails, you can flash the problem and redirect back to the form, keeping every path refresh safe.

if errors:
    flash("Fix the form")
    return redirect(url_for("signup"))

The Full PRG View

Process, then redirect. This PRG shape is the standard for any form that changes data on the server.

@app.route("/comment", methods=["POST"])
def comment():
    save_comment(request.form["text"])
    return redirect(url_for("thanks"))

Quick Check

Why does PRG stop those accidental duplicates?

Recap: PRG Pattern

You processed the POST, redirected to a GET with url_for, used flash for messages, and made every refresh safe. 🎉

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

هل درس «إعادة التوجيه بعد POST (نمط PRG)» مجاني؟

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

ماذا ستتعلم في «إعادة التوجيه بعد POST (نمط PRG)»؟

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

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

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

كم من الوقت يستغرق درس «إعادة التوجيه بعد POST (نمط PRG)»؟

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

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

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

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

  1. بناء نموذج HTML يرسل POST
  2. قراءة معلمات الاستعلام وتعيين قيمها الافتراضية
  3. التحقق اليدوي من الحقول المطلوبة
  4. إعادة التوجيه بعد POST (نمط PRG)
← العودة إلى Flask Academy