0Pricing
Django Academy · บทเรียน

วิดเจ็ต ป้ายกำกับ และข้อความช่วยเหลือ

ควบคุมวิธีแสดงผลของแต่ละฟิลด์

วิดเจ็ต ป้ายกำกับ และข้อความช่วยเหลือ เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Polishing the Form

A working ModelForm is great, but the auto-generated inputs feel plain. You can refine how each field looks and reads with widgets, labels, and help text. 🎨

What a Widget Is

A widget is the HTML control rendered for a field, such as a text box, textarea, or date picker. Swap it to change the input shown.

Overriding Widgets in Meta

Set the widgets dict in Meta to map a field name to a different control. Here a body field becomes a roomy textarea.

class Meta:
    model = Post
    fields = ["body"]
    widgets = {"body": forms.Textarea}

Passing Widget Attributes

Instantiate the widget to pass HTML attrs, like rows or a CSS class. This is how you wire forms into your styling.

widgets = {
    "body": forms.Textarea(attrs={"rows": 4})
}

Adding a CSS Class

The same attrs dict adds a class so your stylesheet can target the input. Great for Bootstrap or your own utility classes.

forms.TextInput(attrs={"class": "form-control"})

Setting a Placeholder

A placeholder hints what to type before the user clicks in. Add it through attrs to guide people gently.

forms.TextInput(attrs={"placeholder": "Enter a title"})

Custom Labels

Django labels fields from their names, but you can override them. The labels dict in Meta gives any field a friendlier caption.

labels = {"body": "Post content"}

Why Labels Matter

Clear labels make a form readable and accessible. A field named pub_date reads far better shown as "Publish date" to your users.

Adding Help Text

The help_texts dict shows a small hint under a field. Use it to explain limits or formatting without cluttering the label.

help_texts = {"title": "Keep it under 60 characters."}

Custom Error Messages

You can even reword validation errors with the error_messages dict, so the wording matches your app's voice.

error_messages = {
    "title": {"required": "Please add a title."}
}

Putting It Together

All of these live side by side in Meta. Mixing widgets, labels, and help_texts gives a clean, branded form with almost no extra code.

class Meta:
    model = Post
    fields = ["title", "body"]
    labels = {"body": "Content"}

Quick Check

Which Meta dict changes the HTML control rendered for a field?

Recap: Customizing Fields

You did it! In Meta you can set widgets, labels, help_texts, and error_messages to make any ModelForm look and read exactly how you want. 🌟

คำถามที่พบบ่อย

บทเรียน “วิดเจ็ต ป้ายกำกับ และข้อความช่วยเหลือ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “วิดเจ็ต ป้ายกำกับ และข้อความช่วยเหลือ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “วิดเจ็ต ป้ายกำกับ และข้อความช่วยเหลือ”

ควบคุมวิธีแสดงผลของแต่ละฟิลด์ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “วิดเจ็ต ป้ายกำกับ และข้อความช่วยเหลือ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การประกาศ ModelForm และ Meta.fields
  2. form.save() และ commit=False
  3. การตรวจสอบข้อมูลแบบกำหนดเองด้วยเมธอด clean
  4. วิดเจ็ต ป้ายกำกับ และข้อความช่วยเหลือ
← กลับไปที่ Django Academy