تعريف forms.Form
صرّح بالحقول وعناصر إدخالها
تعريف forms.Form درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Forms Exist
A Django form turns messy user input into clean Python data, handling rendering and validation in one tidy place. 📝
Subclass forms.Form
You declare a form by subclassing forms.Form. Each class attribute you add becomes a field on that form.
from django import forms
class ContactForm(forms.Form):
name = forms.CharField()Fields Are Class Attributes
Every field is a class attribute set to a field instance, like CharField or EmailField. The attribute name becomes the input name.
name = forms.CharField()
email = forms.EmailField()CharField for Text
Use CharField for short text. Set max_length to cap the size and render a sensible HTML input automatically.
subject = forms.CharField(max_length=120)EmailField Validates Format
An EmailField checks that the value looks like a real email address, so you do not write that validation yourself.
email = forms.EmailField()Required by Default
Every field is required unless you say otherwise. Pass required=False to make a field optional for the user.
phone = forms.CharField(required=False)label and help_text
Add a friendly label and help_text so users understand each field. Django shows them next to the input.
name = forms.CharField(label="Your name",
help_text="First and last")Choosing a Widget
A widget controls the HTML rendered for a field. Swap the default to get a textarea, password box, or select.
message = forms.CharField(
widget=forms.Textarea)More Field Types
Django ships many fields: IntegerField, BooleanField, DateField, and ChoiceField each map to the right input and parse the value for you.
age = forms.IntegerField()
agree = forms.BooleanField()ChoiceField and Options
A ChoiceField renders a dropdown from a list of value and label pairs you provide in choices.
plan = forms.ChoiceField(choices=[
("free", "Free"), ("pro", "Pro")])Instantiate the Form
Create an unbound form with no data to display a blank form, or pass data to bind it for validation.
form = ContactForm() # blank
form = ContactForm(request.POST)Quick Check
Let us see how forms are defined.
Recap
You subclass forms.Form and add fields as class attributes. Pick the right field type and widget, and Django handles the rest. ✅
الأسئلة الشائعة
هل درس «تعريف forms.Form» مجاني؟
نعم — نص درس «تعريف forms.Form» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «تعريف forms.Form»؟
صرّح بالحقول وعناصر إدخالها تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «تعريف forms.Form»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.