django form field class
from django import forms
class MyForm(forms.Form):
my_field = forms.CharField(
max_length=100,
required=True,
widget=forms.TextInput(attrs={'placeholder': 'Enter text'}),
label='My Field'
)
Import the
forms
module from the Django framework.Define a form class named
MyForm
that inherits fromforms.Form
.Create a form field named
my_field
using theCharField
class fromforms
. This field is a text input field.Set the
max_length
attribute of the field to 100, indicating the maximum number of characters allowed.Set the
required
attribute toTrue
to make the field mandatory.Use the
widget
attribute to customize the appearance of the field. In this case, it's a text input with a placeholder attribute set to 'Enter text'.Set the
label
attribute to 'My Field' to specify the label associated with the form field.