简体   繁体   English

如何使用没有WTForms的烧瓶创建动态表单

[英]How to create a dynamic form with flask without WTForms

What I am trying to do is create a form that is automatically generated and then the values from that form are then used to proceed in the program. 我要做的是创建一个自动生成的表单,然后使用该表单中的值继续执行该程序。 Currently to generate my form I am using this function 目前要生成我的表单我正在使用此功能

real_names=["Jason","Casandra"] this portion of the code is not static it is generated with the names of the previous part of the form. real_names=["Jason","Casandra"]这部分代码不是静态的,它是使用表单前一部分的名称生成的。

     form = """
            <h2>What fields do you want your employee's on?:</h2>
            <dl>
            """
            def form_creator(name):
                            print name
                            dt = "<dt>"
                            dt_1 = dt+name.title()+":"
                            dt_kill = "</dt>"
                            input_basic_1 = '''<input name="''' 
                            input_basic_2 = """" size="1"> </input>"""
                            generated_input = dt_1+ input_basic_1 + name.lower() + input_basic_2+dt_kill
                            # flash(generated_input)
                            return generated_input
    for i in real_names:
                print ("\n\n"+i+"\n\n")
                form += form_creator(i)

return render_template('assign_score_b.html',form=form)

The HTML code is HTML代码是

{% extends "assign.html" %}
{% block content %}

<form method="POST">
   {{form|safe}}
</form>

{% endblock %}

Now after I generate the form and then get the varibles from the user I would like to process the form but I cannot seem to figure out how to get the values to be checked. 现在,在我生成表单然后从用户获取变量后,我想处理表单,但我似乎无法弄清楚如何获取要检查的值。 What I was trying to do for the answers was 我试图为答案做的是

answers = []
for i in real_names:
    testable_var = request.form[i]
    try:
        int(testable_var)
    except:
        flash("Please submit an number not a letter")
    answers.append(testable_var)

but when I was trying to do this I got a error request. 但是当我试图这样做时,我收到了一个错误请求。 Using WTForms is NOT an option Your help will be very appreciated Thank you! 使用WTForms 不是一个选项你的帮助将非常感谢谢谢!

your code will work but you made one small crucial mistake. 你的代码会起作用,但你犯了一个小错误。 So your names are "Jason" and "Casandra" but the names of your form are "jason" and "casandra" The names themselves are capitalized but the form values aren't! 所以你的名字是"Jason" and "Casandra"但你的表格的名字是"jason" and "casandra"名字本身是大写的,但形式值不是! Your code will work if you lower the variable i you can do this simply with string manipulation all you have to do is add on .lower() to your variable therefore your code would look like this: 如果你降低变量,你的代码将会起作用i只需使用字符串操作即可完成此操作所有你需要做的就是将.lower()添加到你的变量中,因此你的代码看起来像这样:

for i in real_names:
    testable_var = request.form[i.lower()]
    try:
        int(testable_var)
    except:
        flash("Please submit an number not a letter")
    answers.append(testable_var)

now your code will work! 现在您的代码将起作用!

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

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