Categories
Python Answers

How to add a placeholder on a CharField in Python Django?

Spread the love

Sometimes, we want to add a placeholder on a CharField in Python Django.

In this article, we’ll look at how to add a placeholder on a CharField in Python Django.

How to add a placeholder on a CharField in Python Django?

To add a placeholder on a CharField in Python Django, we can use the forms.CharField class.

For instance, we write

q = forms.CharField(label='search', 
                    widget=forms.TextInput(attrs={'placeholder': 'Search'}))

in our form class to create the q CharField.

We set the placeholder of the CharField by setting the attrs argument to {'placeholder': 'Search'}.

Conclusion

To add a placeholder on a CharField in Python Django, we can use the forms.CharField class.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *