简体   繁体   English

Django crispy-forms - 自定义按钮

[英]Django crispy-forms - Custom button

I have two buttons in my django-crispy-form 我的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? 由于我是django / pyhton的新手,我如何在取消按钮的操作中添加goto url(例如下面的url)?

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

I have read that this is possible with JQuery, but this is something i never used. 我已经读过这可以用JQuery,但这是我从未使用过的东西。 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. 请记住,我使用脆皮形式,并希望尽可能使用ass html。

If jQuery is used; 如果使用jQuery;

  1. How would the jQuery code look like to open a url? jQuery代码如何打开网址?
  2. Do i create a new jQuery file containing the jQuery Code? 我是否创建了一个包含jQuery代码的新jQuery文件?
  3. Do i store jQuery files in my static folder? 我将jQuery文件存储在静态文件夹中吗?
  4. How do i call the jQuery from the button in the crispy form? 我如何从cristy形式的按钮调用jQuery?

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

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. 所以some_cancel_url是你要去的网址的名称

Seems a little hackish, but I think an anchor is better than javascript. 似乎有点hackish,但我认为锚比javascript更好。 Better for search engines, people without JS or simply for checking out the link by hovering. 更适合搜索引擎,没有JS的人或只是通过悬停检查链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sort_order 不会从 Django Crispy 表单中的工具排序按钮更改 - sort_order does not change from toogle sort button in Django Crispy forms 使用Ajax验证并提交Django表单(django-crispy-forms) - validate and submit Django form (django-crispy-forms) with Ajax 如何使用 Ajax 渲染 django-crispy-forms 的验证错误? - How to rendering validation errors of django-crispy-forms with Ajax? 在输入字段内添加一个显示密码按钮 - 脆 forms - add a show password button inside of input field - crispy forms 如何在不刷新页面的情况下使用 django-crispy-forms 实现引导模式表单 - How do I implement bootstrap modal forms with django-crispy-forms, without page refresh 使用 Django 中的按钮关闭所有 forms - Close all forms with a button in Django 脆皮表单输入字段上的jQuery不起作用 - jQuery on Crispy Forms input fields not working 显示三个模型表单并使用django中的一个按钮提交 - Display three Model Forms and submit with single button in django 如何在Django表单中使用自定义小部件(日期时间选择器) - How to use custom widgets in django forms (datetime picker) Django表单输入类型文本在单击“提交”按钮时保持返回值“ on” - Django forms Input type text keeps returning value “on” when clicking on the submit button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM