简体   繁体   English

使用Flask / Flask-WTF的简单动态表单

[英]Simple dynamic forms with Flask/Flask-WTF

I need to build a simple form with a number of checkboxes. 我需要构建一个带有多个复选框的简单表单。 The problem is that I need the checkboxes to come from a csv file which would look like this: 问题是我需要复选框来自csv文件,如下所示:

data_so.csv data_so.csv

Name
A. Blabla
U. Blublu
I. Blibli
O. Bloblo

The form, right now, with some hard coded checkboxes, looks like this: 现在,该表单带有一些硬编码的复选框,如下所示:

在此处输入图片说明

Instead of "Mr. 1", I'd need to have "A. Blabla", instead of "Ms. 2", I'd want "U. Blublu", etc. and instead of 3 checkboxes, I'd need 4, the number of entries in my csv file. 我需要的不是“ Mr. 1”,而是需要“ A. Blabla”,而不是“ Ms. 2”,而是“ U. Blublu”,等等。我需要的不是3个复选框4,我的csv文件中的条目数。

Here are my Flask files: 这是我的Flask文件:

route_so.py route_so.py

from flask import Flask, render_template, request, flash
from forms_so import ContactForm
import csv

app = Flask(__name__)

app.secret_key = 'development key'

@app.route('/', methods=['GET', 'POST'])
def home():
  form = ContactForm()

  if request.method == 'POST':
    if form.validate() == False:
      flash('All fields are required.')
      return render_template('home_so.html', form=form)
    else:
      print(form.node_1.data,form.node_2.data,form.node_3.data)
      return render_template('home_so.html', success=True)

  elif request.method == 'GET':
    return render_template('home_so.html', form=form)

if __name__ == '__main__':
  app.run(debug=True)

form_so.py form_so.py

from flask.ext.wtf import Form
import csv
from wtforms import TextField, RadioField, TextAreaField, SubmitField, validators, BooleanField

class ContactForm(Form):

    # my attempt to force the creation of dynamic global variables
    with open('/data_so.csv', 'rb') as f:
        reader = csv.reader(f)
        r = list(reader)

    nodes = {}
    for i in range(1,len(r)):
        globals()[''.join("node_"+str(i))] = BooleanField(r[i][0])
    # end of my attempt to force the creation of dynamic global variables

    node_1 = BooleanField("Mr. 1")
    node_2 = BooleanField("Ms. 2")  
    node_3 = BooleanField("Dr. 3")
    # this needs to be dynamically set instead

    submit = SubmitField("Send")

So I tried and created dynamic variables (in a dirty, hacky way). 因此,我尝试并创建了动态变量(以肮脏的方式)。 The problem now is that I don't know how to make the home_so.html work with an undifined number of variables... 现在的问题是,我不知道如何使home_so.html使用数量不受限制的变量...

home_so.html home_so.html

{% extends "layout_so.html" %}

{% block content %}

  {% if success %}
    <p>Thank you for filling up our survey. We'll get back to you shortly.</p>

  {% else %}  

    <form action="{{ url_for('home') }}" method=post>
      {{ form.hidden_tag() }}

       <h2>List of check boxes dynamically built from local csv file</h2>

      #this needs to be dynamically set
      {{ form.node_1.label }}
      {{ form.node_1 }}

      {{ form.node_2.label }}
      {{ form.node_2 }}

      {{ form.node_3.label }}
      {{ form.node_3 }}

      {{ form.submit }}
    </form>

  {% endif %}
{% endblock %}

Is there a way to accomplish this sort of things with a simple csv file? 有没有办法通过一个简单的csv文件来完成这种事情? If not, what's the usual way to go about dynamically producing a form as it loads client-side? 如果不是,在加载客户端时动态生成表单的通常方法是什么?

{% for node in node_list_from_app %}
  <p class="field"><label><input type="checkbox" name="node" value="{{ node }}"> {{ node }}</label></p>
{% endfor %}

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

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