简体   繁体   English

Flask/Jinja2 动态 WTF 表单模板

[英]Flask/Jinja2 dynamic WTF form template

I am trying to create a jinaj2 template with a dynamic number of input fields.我正在尝试创建一个具有动态数量的输入字段的 jinaj2 模板。

The form is generating successfully in Flask, however I am having issues with the Jinja/HTML.该表单在 Flask 中成功生成,但是我遇到了 Jinja/HTML 的问题。

As an example of my "non-dynamic" working example I have one standard form field "forID" and and four hard coded field forms field+(field number).作为我的“非动态”工作示例的示例,我有一个标准表单字段“forID”和四个硬编码字段表单字段+(字段编号)。

{{ form.csrf_token }}
{{ wtf.form_field(form.formID) }}

{{ wtf.form_field(form.field1) }}
{{ wtf.form_field(form.field2) }}
{{ wtf.form_field(form.field3) }} 
{{ wtf.form_field(form.field4) }}

What method can I use to make the Jinja/HTML dynamic.我可以使用什么方法使 Jinja/HTML 动态化。 I have tried EVAL and EVEC, but these Python commands are not supported in Jinja.我尝试过 EVAL 和 EVEC,但 Jinja 不支持这些 Python 命令。

#<form method="POST" action = "{{url_for('myHtmlTemplate')}}">
{{ form.csrf_token }}
{{ wtf.form_field(form.formID) }}

{% for i in form.myDynamicFields.keys() %}:
     {{ eval(' wtf.form_field(form.'+'i')') }}
{% endfor %}    

<input class="btn btn-success" type="submit" value="Submit">
#</form>

My current myForm.我当前的 myForm。

class myform(FlaskForm):
connection = removed
result = connection.execute("SELECT * FROM sqltable")
result = result.fetchall()

scenarioList = []
fieldList = []
fieldLabel = []

for scen in result:
scenarioList.append("Scenario: " + scen[0])
fieldList.append("field: " + scen[0])

for i in range(len(result)):
fieldLabel.append("My Field" + str(i + 1))

myDynamicFields = dict(zip(fieldLabel, fieldList))
formID = StringField('Form ID')

for key, val in myDynamicFields.items():
exec(key + '=StringField(val)')

' wtf.form_field(form.'+'i')' has an odd number of apostrophes which is a syntax problem ' wtf.form_field(form.'+'i')'有奇数个撇号,这是一个语法问题

I'm guessing you want something closer to:我猜你想要更接近的东西:

' wtf.form_field(form.' + i + ')'

So that you get这样你就得到

i = 'your_field'
print(' wtf.form_field(form.' + i + ')')

wtf.form_field(form.your_field)

I do agree with @DanielRoseman's comment.我同意@DanielRoseman 的评论。 I would probably approach the problem more like the following (if it works with your implementation):我可能会更像以下解决问题(如果它适用于您的实现):

{% for field in form.myDynamicFields %}:
     {{ wtf.form_field(field) }}
{% endfor %} 

I did not manage to use the method of iterating the dictionary discussed above, although if it could work I agree it would be much more elegant.我没有设法使用上面讨论的迭代字典的方法,尽管如果它可以工作,我同意它会更优雅。

However, I have a work around.但是,我有一个解决方法。 Generating the HTML as a text string in my form instance, starting with a template and dynamically building up the section Jinja2 could not evaluate without errors.在我的表单实例中将 HTML 生成为文本字符串,从模板开始并动态构建 Jinja2 无法评估的部分。 I pass this HTML string out the class into the render function.我将这个 HTML 字符串从类中传递到渲染函数中。

In the meantime I'll contact the Jinja2 development team to see how it could be done within the template itself.与此同时,我将联系 Jinja2 开发团队,看看如何在模板本身内完成。

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

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