Meta Class in Django Form , Meta Class का Django-Model Forms में उपयोग
Meta Class in Django Forms
In Django फ़ॉर्म्स, Meta class model-आधारित फ़ॉर्म को कस्टमाइज़ करने के लिए उपयोग की जाती है। यह आपको फ़ॉर्म को एक मॉडल से लिंक करने और फील्ड्स, लेबल्स, विजेट्स, और अन्य प्रॉपर्टीज़ को परिभाषित करने की अनुमति देती है। This enables advanced customization and helps tailor the form to specific requirements.
Usage (उपयोग)
class UserRegistration(forms.ModelForm):
class Meta:
model = User
fields = ['name', 'email', 'password']
widgets = {
'name': forms.TextInput(attrs={'class': 'form-control'}),
'email': forms.EmailInput(attrs={'class': 'form-control'}),
'password': forms.PasswordInput(attrs={'class': 'form-control'}),
}
labels = {
'name': 'Full Name',
'email': 'Email Address',
}
help_texts = {
'password': 'Choose a strong password.',
}
error_messages = {
'email': {
'invalid': 'Enter a valid email address.',
},
}
Properties of Meta Class (Meta Class की विशेषताएँ)
1. model
Link the form to a particular Model. फ़ॉर्म को एक विशेष मॉडल से लिंक करता है। Example:
model = User
2. fields
Determines what should be the fields in the form to show. फ़ॉर्म में शामिल किए जाने वाले फील्ड्स को तय करता है। Example:
fields = ['name', 'email', 'password']
3. exclude
Determines which fields should not be included (attached) with the form .फ़ॉर्म में शामिल न किए जाने वाले फील्ड्स को तय करता है। Example:
exclude = ['password']
4. widgets
फील्ड्स के HTML आउटपुट को कस्टमाइज़ करता है। Example:
widgets = {
'email': forms.EmailInput(attrs={'class': 'form-control'}),
}
5. labels
फील्ड्स के लिए कस्टम लेबल सेट करता है। Example:
labels = {
'name': 'Full Name',
}
6. help_texts
फील्ड्स के लिए सहायक निर्देश प्रदान करता है। Example:
help_texts = {
'password': 'Choose a strong password.',
}
7. error_messages
वैलिडेशन त्रुटि संदेशों को कस्टमाइज़ करता है। Example:
error_messages = {
'email': {
'invalid': 'Enter a valid email address.',
},
}
Rendered Form Output (HTML आउटपुट)
Warning:
The above Submit button is not coming from the Django Form, you must use the HTML template code for the submit button side.
ऊपर जो Submit बटन दिख रहा है, वह Django Form से नहीं आ रहा है, आपको Submit बटन के लिए HTML टेम्प्लेट कोड का उपयोग करना होगा।
HTML Code for Form (HTML कोड फॉर्म के लिए)
<form method="POST">
{{ csrf_token }}
{{ form }}
<button class="btn btn-primary" type="submit">Submit</button>
</form>
See full Django Docs by clicking here, Django Docs देखने के लिए यहां स्पर्श करें
टिप्पणियाँ
एक टिप्पणी भेजें