[Django]-How do I create a Django form that displays a checkbox label to the right of the checkbox?

2👍

Here’s what I ended up doing. I wrote a custom template stringfilter to switch the tags around. Now, my template code looks like this:

{% load pretty_forms %}
<form action="." method="POST">
{{ form.as_p|pretty_checkbox }}
<p><input type="submit" value="Submit"></p>
</form>

The only difference from a plain Django template is the addition of the {% load %} template tag and the pretty_checkbox filter.

Here’s a functional but ugly implementation of pretty_checkbox – this code doesn’t have any error handling, it assumes that the Django generated attributes are formatted in a very specific way, and it would be a bad idea to use anything like this in your code:

from django import template
from django.template.defaultfilters import stringfilter
import logging

register=template.Library()

@register.filter(name='pretty_checkbox')
@stringfilter
def pretty_checkbox(value):
    # Iterate over the HTML fragment, extract <label> and <input> tags, and
    # switch the order of the pairs where the input type is "checkbox".
    scratch = value
    output = ''
    try:
        while True:
            ls = scratch.find('<label')
            if ls > -1:
                le = scratch.find('</label>')
                ins = scratch.find('<input')
                ine = scratch.find('/>', ins)
                # Check whether we're dealing with a checkbox:
                if scratch[ins:ine+2].find(' type="checkbox" ')>-1:
                    # Switch the tags
                    output += scratch[:ls]
                    output += scratch[ins:ine+2]
                    output += scratch[ls:le-1]+scratch[le:le+8]
                else:
                    output += scratch[:ine+2]
                scratch = scratch[ine+2:]
            else:
                output += scratch
                break
    except:
        logging.error("pretty_checkbox caught an exception")
    return output

pretty_checkbox scans its string argument, finds pairs of <label> and <input> tags, and switches them around if the <input> tag’s type is “checkbox”. It also strips the last character of the label, which happens to be the ‘:’ character.

Advantages:

  1. No futzing with CSS.
  2. The markup ends up looking the way it’s supposed to.
  3. I didn’t hack Django internals.
  4. The template is nice, compact and idiomatic.

Disadvantages:

  1. The filter code needs to be tested for exciting values of the labels and input field names.
  2. There’s probably something somewhere out there that does it better and faster.
  3. More work than I planned on doing on a Saturday.

33👍

Here’s a solution I’ve come up with (Django v1.1):

{% load myfilters %}

[...]

{% for field in form %}
    [...]
    {% if field.field.widget|is_checkbox %}
      {{ field }}{{ field.label_tag }}
    {% else %}
      {{ field.label_tag }}{{ field }}
    {% endif %}
    [...]
{% endfor %}

You’ll need to create a custom template tag (in this example in a “myfilters.py” file) containing something like this:

from django import template
from django.forms.fields import CheckboxInput

register = template.Library()

@register.filter(name='is_checkbox')
def is_checkbox(value):
    return isinstance(value, CheckboxInput)

More info on custom template tags available here.

Edit: in the spirit of asker’s own answer:

Advantages:

  1. No futzing with CSS.
  2. The markup ends up looking the way it’s supposed to.
  3. I didn’t hack Django internals. (but had to look at quite a bunch)
  4. The template is nice, compact and idiomatic.
  5. The filter code plays nice regardless of the exact values of the labels and input field names.

Disadvantages:

  1. There’s probably something somewhere out there that does it better and faster.
  2. Unlikely that the client will be willing to pay for all the time spent on this just to move the label to the right…

15👍

I took the answer from romkyns and made it a little more general

def field_type(field, ftype):
    try:
        t = field.field.widget.__class__.__name__
        return t.lower() == ftype
    except:
        pass
    return False

This way you can check the widget type directly with a string

{% if field|field_type:'checkboxinput' %}
    <label>{{ field }} {{ field.label }}</label>
{% else %}
    <label> {{ field.label }} </label> {{ field }}
{% endif %}
👤Marco

12👍

All presented solutions involve template modifications, which are in general rather inefficient concerning performance. Here’s a custom widget that does the job:

from django import forms
from django.forms.fields import BooleanField
from django.forms.util import flatatt
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.utils.translation import ugettext as _


class PrettyCheckboxWidget(forms.widgets.CheckboxInput):
    def render(self, name, value, attrs=None):
        final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
        if self.check_test(value):
            final_attrs['checked'] = 'checked'
        if not (value is True or value is False or value is None or value == ''):
            final_attrs['value'] = force_text(value)
        if 'prettycheckbox-label' in final_attrs:
            label = _(final_attrs.pop('prettycheckbox-label'))
        else:
            label = ''
        return format_html('<label for="{0}"><input{1} /> {2}</label>', attrs['id'], flatatt(final_attrs), label)


class PrettyCheckboxField(BooleanField):
    widget = PrettyCheckboxWidget
    def __init__(self, *args, **kwargs):
        if kwargs['label']:
            kwargs['widget'].attrs['prettycheckbox-label'] = kwargs['label']
            kwargs['label'] = ''
        super(PrettyCheckboxField, self).__init__(*args, **kwargs)


# usage in form
class MyForm(forms.Form):
    my_boolean = PrettyCheckboxField(label=_('Some label'), widget=PrettyCheckboxWidget())

I have PrettyCheckboxWidget and PrettyCheckboxField in an extra file, so they may be imported where needed. If you don’t need translations, you can remove the ugettext parts. This code works on Django 1.5 and is untested for lower versions.

Advantages:

  • Highly performant, needs no template modifications
  • Easy to use as a custom widget

Disadvantages:

  • “as_table” renders the checkbox + label inside the second column
  • {{ field.label }} inside the template is empty. The label is instead bound to {{ field }}
  • More work than I planned on doing on a Saturday 😉

4👍

I know that the user excluded CSS, but considering the top answers take about half hour of work to do such a small thing, but knowing that details like these are important on a website, I’d settle for the CSS solution.

checkbox.css

input[type="checkbox"] {
    float: left;
    margin-right: 10px;
    margin-top: 4px;
}

forms.py

class MyForm(forms.ModelForm):
    # ...
    class Media:
    css = {
        'all': 'checkbox.css',
    }

template.html

{{ form.media }}
{{ form.as_p }}

Advantages:

  • fast!
  • no futzing with template tags (just form.as_p)
  • no new damned widgets
  • the CSS file is automatically included in every form

Disadvantages:

  • the HTML doesn’t reflect the presentation (but is good enough!)
  • your frontendist could complain

2👍

Changing checkbox position in Django admin can be quite tricky, but luckily there is a simple solution using custom widget:

from django.forms.widgets import Widget, CheckboxInput, boolean_check

class RightCheckbox(Widget):
    render = CheckboxInput().render

    def __init__(self, attrs=None, check_test=None):
        super(RightCheckbox, self).__init__(attrs)
        self.check_test = boolean_check if check_test is None else check_test

Django uses left position only when widget is subclass of CheckboxInput

👤Andy

1👍

Order of inputs and labels is provided via normal_row parameter of the form and there are no different row patterns for checkboxes. So there are two ways to do this (in 0.96 version exactly):
1. override _html_output of the form
2. use CSS to change position of the label and checkbox

0👍

I had this problem too and I just used this method :

post-detail.html

{% if field.html_name == "check_box" %}
      {{ field }}
     <label for="{{ field.id_for_label }}" class="form-label">
    {{ field.label }}
    </label>
{% else %}
   <label for="{{ field.id_for_label }}" class="form-label">
   {{ field.label }}
   </label>
  {{ field }}
{% endif %}

Leave a comment