เรนเดอร์และส่งแบบฟอร์ม
แสดงฟิลด์ในเทมเพลตและจัดการการส่ง
เรนเดอร์และส่งแบบฟอร์ม เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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.dataAuto-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 POSTOne 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. 🎉
คำถามที่พบบ่อย
บทเรียน “เรนเดอร์และส่งแบบฟอร์ม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เรนเดอร์และส่งแบบฟอร์ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เรนเดอร์และส่งแบบฟอร์ม”
แสดงฟิลด์ในเทมเพลตและจัดการการส่ง คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “เรนเดอร์และส่งแบบฟอร์ม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- กำหนดคลาส FlaskForm
- เรนเดอร์และส่งแบบฟอร์ม
- validate_on_submit และโทเคน CSRF
- ตัวตรวจสอบแบบกำหนดเองและข้อผิดพลาดของฟิลด์