Custom CSS to Django form inputs
Tags: django, python
Published: 2022-08-13 07:52:47

---
title: Custom CSS to Django form inputs
published: true
date: 2022-08-13 07:49:48 UTC
tags: Django, Python
canonical_url: https://www.rockandnull.com/django-form-css/
---

 ![Custom CSS to Django form inputs](https://images.unsplash.com/photo-1484480974693-6ca0a78fb36b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDIyfHxzdXJ2ZXl8ZW58MHx8fHwxNjYwMzc2ODgw&ixlib=rb-1.2.1&q=80&w=2000)

I think rendering an HTML form for you is one of Django's biggest conveniences.

The downside is that the structure of the generated form is not customizable. In case you have a specific design and you need the placement of the inputs at a different position, Django gives you a way to [render each element individually](https://docs.djangoproject.com/en/4.1/topics/forms/#rendering-fields-manually) at the input control level as follows:

```
<div class="fieldWrapper">
    {{ form.subject.errors }}
    <label for="{{ form.subject.id_for_label }}">Subject:</label>
    {{ form.subject }}
</div>
```

_template.html_

But a customization problem still remains: you cannot set custom classes to the individual input controls. The `{{ form.subject }}` will render the input control without giving you the chance to set a custom class.

This is problematic because most CSS frameworks (e.g. [Bootstrap](https://getbootstrap.com/), [Bulma](https://bulma.io/)) require you to set specific classes to input controls to style them appropriately.

One solution is to set these classes in the `Form` object:

```
class ExampleForm(forms.Form):
   subject = forms.CharField(
       widget=forms.TextInput(attrs={'class':'input-text'}))
   enabled = forms.BooleanField(
       widget=forms.TextInput(attrs={'class':'input-checkbox'}))
```

_forms.py_

But in my opinion, this creates a different issue. The `Form` object is supposed to model the form with its inputs' data types and any other business logic requirements. It's not supposed to be responsible for styling and other visual attributes. The place for such styling instructions is the template.

Unfortunately, I think there's no built-in way of doing this in Django without using an external library. But fear not. There is such a small and lightweight library that does exactly that called [django-widget-tweaks](https://pypi.org/project/django-widget-tweaks/).

Using this library you can set custom CSS classes (and more) to your input controls, and you can set this in the template, where all the positioning and styling of the form logically belongs to:

```
{% load widget_tweaks %}

<div class="fieldWrapper">
    {{ form.subject.errors }}
    <label for="{{ form.subject.id_for_label }}">Subject:</label>
    {{ form.subject|add_class:"input-text" }}
</div>

<div class="fieldWrapper">
    {{ form.enabled.errors }}
    <label for="{{ form.enabled.id_for_label }}">Enabled:</label>
    {{ form.enabled|add_class:"input-checkbox" }}
</div>
```

_template.html_

The library offers quite a few more options in customizing form controls. If this has piqued your interest, have a look!

Hopefully, you learned a way to customize Django forms without mixing styling instructions in the `Form` model.

Happy coding!