بناء نموذج HTML يرسل POST
أنشئ نموذجًا يوجّه action فيه إلى مسار.
بناء نموذج HTML يرسل POST درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Forms Matter
An HTML form is how a browser collects typed input and hands it to your server. It is the front door for almost every user action. 📝
The form Element
Everything starts with a form tag. The browser bundles whatever fields live inside it and sends them together when the user submits.
<form>
<!-- fields go here -->
</form>Point It With action
The action attribute names the URL that receives the data. Leave it empty to post back to the same page the form lives on.
<form action="/signup">Choose the method
Set method to post so the values travel in the request body, not the URL. That keeps form data tidy and out of browser history.
<form action="/signup" method="post">Name Every Field
Each input needs a name attribute. That name becomes the key Flask uses to find the value on the server side.
<input type="text" name="username">Add a Submit Button
A submit button triggers the send. Clicking it tells the browser to gather the named fields and fire the request to your action URL.
<button type="submit">Sign up</button>Match the Route Methods
The receiving route must allow POST. List it in methods, or Flask answers with a 405 and your form data never lands.
@app.route("/signup", methods=["GET", "POST"])
def signup():
...Read Values From request.form
On the server, posted fields live in request.form, a dict-like object keyed by each input name you defined in the HTML.
username = request.form["username"]Serve the Form on GET
When the request is a GET, just render the page that shows the empty form so users have something to fill out.
if request.method == "GET":
return render_template("signup.html")Handle the POST Branch
When the method is POST, read the fields and act on them. One view can both show the form and process the submission.
if request.method == "POST":
name = request.form["username"]A Complete Mini Form
Put it together: a form that posts to /signup and a view that greets the user. That is the full round trip in a few lines.
@app.route("/signup", methods=["GET", "POST"])
def signup():
if request.method == "POST":
return "Hi " + request.form["username"]
return render_template("signup.html")Quick Check
Let us confirm what makes a form actually post.
Recap: Posting Forms
You built a form with an action and post method, named your fields, allowed POST on the route, and read values from request.form. 🎉
الأسئلة الشائعة
هل درس «بناء نموذج HTML يرسل POST» مجاني؟
نعم — نص درس «بناء نموذج HTML يرسل POST» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «بناء نموذج HTML يرسل POST»؟
أنشئ نموذجًا يوجّه action فيه إلى مسار. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «بناء نموذج HTML يرسل POST»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- بناء نموذج HTML يرسل POST
- قراءة معلمات الاستعلام وتعيين قيمها الافتراضية
- التحقق اليدوي من الحقول المطلوبة
- إعادة التوجيه بعد POST (نمط PRG)