简体   繁体   中英

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 (eg 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?

我想通了,我使用了以下内容;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM