[Django]-Django Crispy Forms – Add Button via Helper

2πŸ‘

βœ…

This is not included by default (afaik..).
If you just need this once, is possible to use crispy-forms HTML Layout Object

HTML('<input type="button" name="Save" onclick="do_whatever" />')

What do you then dislike using jQuery? You could handle this rather easy and generic, using something like:

$('form :submit.ajax_submit').live('click', function(e) {
    e.preventDefault();
    var my_form = $(this).parents('form');

    // do whatever
    alert(my_form.attr('id'));
    alert(my_form.attr('action'));
});

and then just pass the class:

Submit('save', 'save', css_class='ajax_submit')
πŸ‘€ohrstrom

5πŸ‘

Are you certain that Button will not take onclick in its kwargs?

I just added an onclick="javascript here" to a Submit() element and it displayed fine.

I also peered a bit at the underlying code, and I think all inputs by default flatten the kwargs that aren’t popped because of special use (i.e. template), and pass those through into the rendered HTML. This may be new since April ’12 (when originally posted), but it currently seems to be as simple as the following:

self.helper.add_input(Button('back', "Back", css_class='btn', onclick="alert('Neat!');"))
πŸ‘€jlovison

Leave a comment