phicon
phicon

Reputation: 3617

Django crispy-forms - Custom button

I have two buttons in my django-crispy-form

    self.helper.add_input(Submit('submit', 'Submit'))
    self.helper.add_input(Button('cancel', 'Cancel'))

The submit button works just fine, the cancel button does nothing. As i am quite new to django/pyhton, how would i add a goto url (e.g. url below) to the action of the cancel button?

url(r'^customeroverview/$', 'customer.views.customeroverview', name='customeroverview'),

I have read that this is possible with JQuery, but this is something i never used. Eager to learn so if this would be the best option i am open to suggestions. Keep in mind i use the crispy form and want to use ass little html as possible.

If jQuery is used;

  1. How would the jQuery code look like to open a url?
  2. Do i create a new jQuery file containing the jQuery Code?
  3. Do i store jQuery files in my static folder?
  4. How do i call the jQuery from the button in the crispy form?

Upvotes: 3

Views: 2723

Answers (2)

Mark
Mark

Reputation: 19969

This page has another way:

HTML("""<a role="button" class="btn btn-default"
    href="{% url "some_cancel_url" %}">Cancel</a>"""),

So some_cancel_url is the name of the url you want to go to.

Seems a little hackish, but I think an anchor is better than javascript. Better for search engines, people without JS or simply for checking out the link by hovering.

Upvotes: 2

phicon
phicon

Reputation: 3617

I figured it out, I used the following;

self.helper.add_input(Button('cancel', "Cancel", css_class='btn', onclick="javascript:location.href = '/customeroverview';"))

Upvotes: 4

Related Questions