0Pricing
Flask Academy · درس

تصيير نموذج وإرساله

اعرض الحقول في قالب وعالج الإرسال.

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

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

From Class to Page

A form class is useless until a user can see it. Now you will render those fields into HTML and accept the submission back. 🖋️

Pass the Form to the Template

Create the form in your view and hand it to render_template. The template gets a live object full of fields it can draw.

form = LoginForm()
return render_template('login.html', form=form)

Open the Form Tag

In Jinja, start with an HTML form tag set to method post. That tells the browser to send field values in the request body.

<form method="post">
  ...
</form>

Render the Hidden Tag

Call form.hidden_tag() first inside the form. It drops in the CSRF token your post will need to be accepted.

<form method="post">
  {{ form.hidden_tag() }}
</form>

Draw a Field

Each field renders itself when you call it. Use field.label for the text and the field itself for the input element.

{{ form.username.label }}
{{ form.username() }}

Pass HTML Attributes

Calling a field accepts keyword arguments that become HTML attributes. Add a class or placeholder right from the template.

{{ form.username(class="input", placeholder="Your name") }}

Render the Submit Button

The submit field draws the button. Calling form.submit() produces the input that posts the whole form.

{{ form.submit() }}

Allow POST in the Route

By default a route only answers GET. List methods so the same view can serve the page and receive the post.

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()

Read Submitted Data

After a post, each field exposes a .data attribute holding the typed value, already coerced to the right Python type.

username = form.username.data
password = form.password.data

Auto-Populate on Reload

Flask-WTF fills fields from the request automatically. If you re-render after a post, the user's input stays in place.

form = LoginForm()  # picks up posted data on POST

One Template, Both Verbs

A single view handles GET and POST: build the form, process it on post, and re-render the same page otherwise.

form = LoginForm()
if form.is_submitted():
    pass  # handle data
return render_template('login.html', form=form)

Quick Check

What must you render inside the form to include the CSRF token?

Recap

You rendered fields with labels and attributes, added hidden_tag for CSRF, allowed POST, and read values from each field's .data. 🎉

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

هل درس «تصيير نموذج وإرساله» مجاني؟

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

ماذا ستتعلم في «تصيير نموذج وإرساله»؟

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

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

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

كم من الوقت يستغرق درس «تصيير نموذج وإرساله»؟

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

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

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

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

  1. تعريف فئة FlaskForm
  2. تصيير نموذج وإرساله
  3. validate_on_submit ورموز CSRF
  4. قواعد التحقق المخصصة وأخطاء الحقول
← العودة إلى Flask Academy