Django’s forms provide a convenient way to create web forms for your applications. The CharField is one of the most commonly used field types, allowing you to define text inputs. Sometimes you may want to add a placeholder value to your CharFields, which shows example input text before the user starts typing. Django doesn’t have a built-in placeholder option for CharFields, but it’s easy to add this functionality. In this post, we’ll look at a few different methods for adding placeholders to Django CharFields.
Using the Placeholders Argument
One straightforward way to add placeholders is by using the placeholder
argument when defining your form fields. For example:
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(max_length=100, placeholder="Your name")
email = forms.EmailField(placeholder="Your email")
message = forms.CharField(widget=forms.Textarea, placeholder="Your message")
This will render HTML placeholder
attributes on the form fields, providing an unobtrusive way to add example values for users.
The placeholder text will be displayed in light gray by default. You can customize the styling by writing some custom CSS.
One downside to this technique is that the placeholder values are not available when processing the form data on the server side. The placeholders exist purely for display in the HTML.
Subclassing the CharField
For full control over the placeholder functionality, you can subclass CharField and add your own PlaceholderCharField:
from django import forms
class PlaceholderCharField(forms.CharField):
def __init__(self, placeholder, *args, **kwargs):
super().__init__(*args, **kwargs)
self.placeholder = placeholder
def widget_attrs(self, widget):
attrs = super().widget_attrs(widget)
attrs['placeholder'] = self.placeholder
return attrs
We add a new placeholder
parameter and store it as an attribute on the field instance. Then we override the widget_attrs
method to add the placeholder value to the list of widget attributes.
Now we can use our new field like this:
from .fields import PlaceholderCharField
class ContactForm(forms.Form):
name = PlaceholderCharField(max_length=100, placeholder="Your name")
email = forms.EmailField(placeholder="Your email")
message = forms.CharField(widget=forms.Textarea, placeholder="Your message")
This gives us full control over the placeholder functionality from within the Form and Field classes, at the cost of some extra complexity.
Adding Placeholders to the Widget
We can also create a custom Widget subclass to handle displaying placeholders:
“`python
from django import forms
class PlaceholderTextInput(forms.TextInput):
def __init__(self, placeholder, *args, **kwargs):
super().__init__(*args, **kwargs)
self.placeholder = placeholder
def render(self, name, value, attrs=None, renderer=None):
input_html = super().render(name, value, attrs=attrs)
return mark_safe(f'<div class="placeholder-text">{self.placeholder}</div>{input_html}')
class ContactForm(forms.Form):
name = forms.CharField(
max_length=100,
widget=PlaceholderTextInput(“Your name”)
)
# Fields omitted for brevity
“`
Here we subclass TextInput and add our placeholder text by overriding the widget’s render method. This wraps the rendered input HTML with a containing the placeholder text. Styling the with CSS will take care of making the placeholder text visible only when the input is empty.
While this approach requires customizing the widget, it keeps the placeholder handling encapsulated there rather than throughout the Form and Field classes.
Conclusion
Adding placeholders to Django CharFields takes a bit more work than some other frameworks which have built-in placeholder support. But Django’s flexibility allows us to achieve placeholders in different ways, choosing the approach that best fits our application’s needs.
The placeholder argument provides a quick and simple way to add static placeholder text. For more control over rendering and processing, custom Field and Widget subclasses give you the ability to implement placeholders exactly how you want them.
Hopefully this gives you some options to consider the next time you need to add placeholders to your Django forms! Proper placeholders can improve the user experience and clarity of your web forms.