add buttons to crispy forms

Date: April 18th 2016
Last updated: April 18th 2016

I wanted to add a button on an "update profile page" that would redirect the user to change their password. Go to PasswordChangeFrom to see how the password change form was created. There are several options to do this, including the "onclick" function from JavaScript.

The example I used to create my button came from here: http://stackoverflow.com/questions/26298332/django-crispy-forms-custom-button

forms.py

class SurferForm(forms.ModelForm):
    #<- snipped ->

    def __init__(self, *args, **kwargs):
        super(SurferForm, self).__init__(*args, **kwargs)
        #<- snipped ->
        self.helper = FormHelper()
        self.helper.add_input(Submit('submit', u'Submit'))
        # ============= SOLUTION ===============
        # note that the url pattern for the changepassword page
        # is located at "/changepassword"
        self.helper.add_input(Button('changepassword', 
                                     "Change password",
                                     css_class='btn',
                                     onclick="javascript:location.href = \
                                     '/changepassword';"))
        #=======================================
    class Meta:
        model = Surfer

results matching ""

    No results matching ""