jinja2 form render不允许包含“ - ”的属性 [英] jinja2 form render does not allow attribute which contains "-"

查看:149
本文介绍了jinja2 form render不允许包含“ - ”的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据自定义表单模板本教程。据我所知, render()只需向标签添加一些属性即可。例如,我添加 placeholder =abc,它的工作正常。

I am trying to customize the form template base on this tutorial. As I understand, render() just add some attributes to the tag. For example, I add placeholder = "abc" and it works well.

{% call inserttourbus(id = "formAddNewRow" )  %}

     <div class="fieldWrapper">
         {% if inserttourbus['bustype'].label() %}Bus Type{% endif %}
         {{ inserttourbus['bustype'].render(placeholder="abc")|safe }}
         {% if inserttourbus['bustype'].errors() %}Not filled yet!{% endif %}      
     </div>
{% endcall %}

这是我的问题:
- 我使用 bootstrap typeahead 为我的模板,所以我需要添加以下属性到 inserttourbus 文本框

Here is my problem: - I use bootstrap typeahead for my template so I need to add the following attribute to the inserttourbus textbox

data-provide="typeahead" data-items="4" data-source='["Alabama","Alaska"]'

所以这将成为

{{ inserttourbus['bustype'].render(placeholder="abc", data-provide="typeahead", data-items="4", data-source='["Alabama","Alaska"]')|safe }}

但是jinja2引擎似乎不接受数据 - 提供数据项,因为它包含 - 字符。如果我将 data-offers 更改为 dataprovide ,那么jinja2引擎可以很好地呈现代码。

But the jinja2 engine seems does not accept data-provide, data-items, so on because it contain "-" character. If I changed data-provide to dataprovide, the jinja2 engine can render the code well.

但是,在 bootstrap typeahead javascript中,所有变量都定义为 data-provide 数据项。如果我将它们更改为 dataprovide dataitems ,则javascipt停止工作。

However, in bootstrap typeahead javascript, all variables are defined as data-provide, data-items. If I change them to dataprovide, dataitems, the javascipt stop working.

请给我一个解决方案:
- 如何使jinja2接受属性具有 -
- 其他解决方案,建议

Please give me a solution: - How to make jinja2 accept attribute which has "-" - Other solutions, advices

推荐答案

在Flask中查看此代码段。我想象,Django的工作方式是一样的;在专用字典中传递无效的Jinja2(Python)语法的HTML属性:

Check out this snippet for doing this in Flask. I imagine it would work the same way for Django; pass HTML attributes with invalid Jinja2 (Python) syntax inside an ad-hoc dictionary:

{{ inserttourbus['bustype'].render(placeholder="abc", 
       **{'data-provide':'typeahead',
          'data-items':'4',
          'data-source':'["Alabama","Alaska"]'}) }}

这篇关于jinja2 form render不允许包含“ - ”的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆