Showing BooleanField checkbox on left

1,259 views
Skip to first unread message

Sayse

unread,
Jan 13, 2016, 9:30:08 AM1/13/16
to Django users, michael...@romaxtech.com

I'm in the process of trying to minimize the amount of code I need to use to render a form with bootstrap styling with the hope of rendering with just {{ form }} but I haven't yet managed to find a way to render a BooleanField with the checkbox before the text.

 

from django.forms import Form, BooleanField

class MyForm(Form):
    field
= BooleanField(label='Test Label')

MyForm().as_table()

 

The above test code will output

    

<tr><th><label for="id_field">Test Label:</label></th><td><input class="" id="id_field" name="field" type="checkbox" /></td></tr>

But what I'm hoping to achieve is the same look and feel as shown in the bootstrap docs.

    

<label for="id_field"><input class="" id="id_field" name="field" type="checkbox" />Test Label:</label>

 The problem in doing this is that the rendering is handled via the form, where the label and the field are positioned/rendered separately as shown in the source, , and I have yet to find a place to override that will allow me to render the widget inside of the label...

 

Any ideas how I can achieve this?

Luis Zárate

unread,
Jan 14, 2016, 9:46:07 AM1/14/16
to django...@googlegroups.com
Do you know django crispy forms ?

http://django-crispy-forms.readthedocs.org/en/latest/
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c92f9f04-6c72-4d2d-8ff4-6de34be9f00e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Sayse

unread,
Jan 14, 2016, 12:55:41 PM1/14/16
to Django users
Yea I've heard of it. Have they managed to achieve this? I'd be interested to see how they've done it if so...

I was hoping to find a way to do this with native django if possible

Tony Flury

unread,
Jan 15, 2016, 7:47:01 PM1/15/16
to Django users


On Thursday, 14 January 2016 17:55:41 UTC, Sayse wrote:
Yea I've heard of it. Have they managed to achieve this? I'd be interested to see how they've done it if so...

I was hoping to find a way to do this with native django if possible


One way that might work is to subclass the CheckboxInputWidget, and in the new widget overide the render method - but there looks like a lot of deep Django Magic in that method : https://docs.djangoproject.com/en/1.9/_modules/django/forms/widgets/#CheckboxInput

Or - look at changing the Form render method - again involving lots of Django Magic

Or - Accept that you can't do that (as you are trying to including one widget (Input) inside the HTML tags for another (Label), and compromise with manual ordering and being more explicit in your form : For instance using {{ field.label_tag }} Example here : https://docs.djangoproject.com/en/1.9/topics/forms/#looping-over-the-form-s-fields

Sayse

unread,
Jan 18, 2016, 4:26:00 AM1/18/16
to Django users
Thanks for your reply..

Subclassing the widget won't help because the widget has no knowledge about its label. I'd gladly override the form render method if I could find out where that would need to be but the source link I provided in my first snippet would make this seem as its not possible since the label is just a piece of string formatting. The manual formatting is what I currently have to use but I should think that it should be possible to do this even though it doesn't appear as though it is currently.
Reply all
Reply to author
Forward
0 new messages