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 from forms.Form.

  • Create a form field named my_field using the CharField class from forms. 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 to True 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.