How to Add a Label to a Form Field in a Django Form 


               


How to Add a Label to a Form Field in a Django Form



Python


In this article, we show how to add a label to a form field in a Django form.

The label is the text that appears, usually to the left of the form field, that tells a user what to enter into the form field.

Even though text will appear by default if no label is added (the text will be the name of the form field) many times you want to create your own custom label.

To add a label to a form field of a Django form, then you just use the label attribute and set it equal in single or double quotes to the text that you want the label to consist of.

Below we add a label to the favorite_fruit form field of a Django form.



So we create a form called FruitFavorite.

We create a form field called favorite_fruit.

This form field is of type, CharField, which mean it accepts text as input.

Inside of this CharField, we specify, label='What is your favorite fruit?' This is the text that will appear to the left of the text field to the user.

If you did not have this label attribute added, then the text to the left of the form field would have been, Favorite fruit.

However, you want to customize to ask a question, What is your Favorite Fruit?

Therefore, for this form field, you would want to create your own custom label, and above is how we can do that.

So this is just quickly how to add a custom label to a form field in a Django form.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...