1

Is there any way to create a widget that generates twitter bootstrap buttons? I am needing to put some buttons in the middle of the form. (or html that are not inputs)

Tried as follows:

class BootstrapButtonWidget(forms.Widget):
    def render(self, value, attrs=None):
        return '<a href="#" class="btn btn-danger delete">%s</a>' % (value,)


class BootstrapButton(forms.Field):
    def __init__(self, value,  *args, **kwargs):
        if not kwargs:
            kwargs = {}
        kwargs["widget"] = BootstrapButtonWidget

        super(BootstrapButton, self).__init__(*args, **kwargs)

    def clean(self, value):
        return value

But I could not pass values​​. He always spends a "value".

I need to render a button like this:

<button type="button" class="btn %s">%s</button> % (btn_type, btn_text)

Rendered:

<button type="button" class="btn btn-primary">Delete this</button>

http://twitter.github.com/bootstrap/base-css.html#buttons

slugonamission
  • 9,562
  • 1
  • 34
  • 41
Matheus Lima
  • 261
  • 1
  • 5
  • 8
  • To mee this does not seem to be a good idea to render buttons as part of django `Form`. The idea of `Form` fields is that they take a value, which they have to validate, and if valid, normalize to consistent format. Buttons however is more or less a constant concept. They do take as input a value which they have to validate. In other words, this might work to solve your issue of rendering buttons in the middle of the form, but conceptually this does not seem to be clean solution. – miki725 Sep 13 '12 at 05:40
  • Not sure if crispy forms can do that but might be useful to you: https://github.com/maraujop/django-crispy-forms – miki725 Sep 13 '12 at 05:43

1 Answers1

1

I assume you're source is referencing http://djangosnippets.org/snippets/2312/ or vice versa for your custom field. I missed the documentation on use, but at the top of the file it states:

"""
Usage:
SubmitButtonField(label="", initial=u"Your submit button text")
"""

If you want a custom value or style, you must change the initial value you pass your BootstrapButton field.

Nielsvh
  • 1,151
  • 1
  • 18
  • 31