0Pricing
Django Academy · درس

Widgets والعناوين ونصوص المساعدة

التحكم في طريقة عرض كل حقل

Widgets والعناوين ونصوص المساعدة درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. 🌟

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

هل درس «Widgets والعناوين ونصوص المساعدة» مجاني؟

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

ماذا ستتعلم في «Widgets والعناوين ونصوص المساعدة»؟

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

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

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

كم من الوقت يستغرق درس «Widgets والعناوين ونصوص المساعدة»؟

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

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

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

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

  1. تعريف ModelForm و Meta.fields
  2. form.save() و commit=False
  3. التحقق المخصص باستخدام أساليب clean
  4. Widgets والعناوين ونصوص المساعدة
← العودة إلى Django Academy