Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 149221

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:05:46+00:00 2026-05-11T09:05:46+00:00

When I define a Django form class similar to this: def class MyForm(forms.Form): check

  • 0

When I define a Django form class similar to this:

def class MyForm(forms.Form):     check = forms.BooleanField(required=True, label='Check this') 

It expands to HTML that looks like this:

<form action='.' id='form' method=POST> <p><label for='check'>Check this:</label> <input type='checkbox' name='check' id='check' /></p> <p><input type=submit value='Submit'></p> </form> 

I would like the checkbox input element to have a label that follows the checkbox, not the other way around. Is there a way to convince Django to do that?

[Edit]

Thanks for the answer from Jonas – still, while it fixes the issue I asked about (checkbox labels are rendered to the right of the checkbox) it introduces a new problem (all widget labels are rendered to the right of their widgets…)

I’d like to avoid overriding _html_output() since it’s obviously not designed for it. The design I would come up with would be to implement a field html output method in the Field classes, override the one for the Boolean field and use that method in _html_output(). Sadly, the Django developers chose to go a different way, and I would like to work within the existing framework as much as possible.

CSS sounds like a decent approach, except that I don’t know enough CSS to pull this off or even to decide whether I like this approach or not. Besides, I prefer markup that still resembles the final output, at least in rendering order.

Furthermore, since it can be reasonable to have more than one style sheet for any particular markup, doing this in CSS could mean having to do it multiple times for multiple styles, which pretty much makes CSS the wrong answer.

[Edit]

Seems like I’m answering my own question below. If anyone has a better idea how to do this, don’t be shy.

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:05:47+00:00Added an answer on May 11, 2026 at 9:05 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the (deprecated) browser attribute: if ($.browser.msie &&… May 12, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer If selecting by Id, you should use : function castvote()… May 12, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer Find / # Signifies the start of a regex expression… May 12, 2026 at 1:11 am

Related Questions

I'm considering using Django for a project I'm starting (fyi, a browser-based game) and
I'm still getting to grips with Django and, in particular, Forms. I created MyForm
If I have a Django form such as: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message
Weird problem in Django with forms : I have a Form.class defined like this

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.