如何转义 django-widget-tweaks 的 render_field 标签中的单引号?

How do I escape single quotes in a django-widget-tweaks's render_field tag?

我正在尝试使用 django-widget-tweaks 呈现以下表单字段:

{% render_field profile_form.bio class+="form-control" id="bio" rows="3" oninput="showBtn('updateProfile')" %}

进入这个:

<textarea class="form-control" id="bio" rows="3" oninput="showBtn('updateProfile')"></textarea>

但是,由于 Django 正在将小部件中的单引号更改为双引号,我遇到了一个解析错误。

showBtnjs函数如下:

// Shows a button given an id
showBtn: function (selector) {
  let btn = document.getElementById(selector);
  btn.classList.remove("btn-hidden");
}

我使用 django-widget-tweaks 的原因是为了在模板中隔离所有 html 类 和属性。

我已经尝试过的:

供参考,这里是表格模型:

# forms.py
class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ["bio"]

和原始模型:

# models.py
class Profile(models.Model):
    """ Non-auth related user information about an user"""
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True)

好的...这就是我最终解决问题的方法。

包的模板标签有效,而不是使用类似于普通 HTML 属性的语法(这就是我首先喜欢 django-widget-tweaks 的原因)。但这就像 django-widget-tweaks 的某些错误的解决方法。

{% render_field profile_form.bio|add_class:"form-control"|attr:"id:bio"|attr:"rows:3"|attr:"oninput:showBtn('updateProfile')" %}

这样就可以正确解析 oninput 属性。