简体   繁体   English

Django Crispy Forms添加HTML属性

[英]Django Crispy Forms add HTML Attributes

My goal is to have LastPass ignore all input forms on my site. 我的目标是让LastPass忽略我网站上的所有输入表单。 I am using crispy forms and showing all form fields with only this in my HTML: 我正在使用松脆的表单,并在HTML中仅显示以下表单字段:

{{ form|crispy }}

However, I want to edit some of the HTML attributes of all input elements created by this form WITHOUT writing each one individually. 但是,我要编辑此表单创建的所有输入元素的一些HTML属性,而无需单独编写每个属性。 LastPass recommends that I use data-lpignore="true" in my HTML input tags. LastPass建议我在HTML input标签中使用data-lpignore="true" I tried to implement that in my form tags, but that didn't work (yes, I know, form != input). 我尝试在表单标签中实现该功能,但这没有用(是的,我知道表单!=输入)。 :

<form action="{{ action }}" method="post" class="no-ajax" data-lpignore="true">
  {% csrf_token %}
  {{ form|crispy }}
  <input type="submit" name="submit" value="Save" class="btn btn-primary btn-lg" />
</form>

This didn't work. 这没用。

I also tried including the following in SETTINGS.py : 我还尝试在SETTINGS.py以下内容:

CRISPY_CLASS_CONVERTERS = {'data-lpignore': 'true'}

Which is supposed to add data-lpignore="true" to my crispy form input tags according to crispy forms' documentation , but this didn't work. 根据crispy form的文档 ,应该将data-lpignore="true"到我的crispy form input标签中,但这不起作用。

Am I stuck doing individual input lines or is there a better solution? 我是否坚持使用单独的输入线,还是有更好的解决方案?

In your form class, pass default attributes to the widget used to render your form fields. 在表单类中,将默认属性传递给用于呈现表单字段的小部件。 For example, 例如,

from django import forms

DEFAULT_ATTRS = {'data-lpignore': 'true'}
...
class SampleForm(forms.Form):
    name = forms.CharField(widget=forms.TextInput(attrs=DEFAULT_ATTRS))

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

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