在 Flask 的页面上多次使用相同的 WTForms 字段

Use same WTForms field multiple times on a page in Flask

我想在一个页面上多次使用 TimeEntryForm 中的 day 字段(次数等于项目数 * 一个月中的天数)。问题是我如何 return 将这些值中的每一个写入数据库。

表格 (forms.py)

class TimeEntryForm(FlaskForm):
    day = StringField('Day')
    submit = SubmitField('Submit')

这部分需要我在代码中放置注释的帮助... 查看 (routes.py)

import datetime
import calendar
@app.route('/enter_time/<year>/<month>', methods=['GET', 'POST'])
@login_required
def enter_time(year, month):
    yy=int(year)
    MM = int(month)
    form = TimeEntryForm()
    projects = Project.query.filter_by(company_id=current_user.company_id).filter_by(is_active='Active').all()
    user = User.query.filter_by(id=current_user.id).first()
    cal = calendar.Calendar()
    date = []
    day_of_week = []
    days = cal.itermonthdates(yy, MM)
    start_date = datetime.date(yy, MM, 1)
    if MM <12:
        end_date = datetime.date(yy, MM+1, 1)
    else:
        end_date = datetime.date(yy,MM, 31)
    for day in days:
        date.append(day)
        day_of_week.append(day.weekday())
    n_weeks = day_of_week.count(0)
    if form.validate_on_submit():
        print(form.data)

        ###This prints out just the "day" for the first form rendered.
        ###How do I get my full list of results in here

        message = 'I worked.'
        flash(message)

    return render_template('enter_time.html', form=form,
                                                projects=projects,
                                                user=user,
                                                date=date,
                                                day_of_week=day_of_week,
                                                start_date=start_date,
                                                end_date=end_date,
                                                n_weeks=n_weeks)

这是我的 HTML:

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

<br>
<form method="POST" enctype = "multipart/form-data">
    {{ form.hidden_tag() }}
    <table class='table table-bordered'>
      <thead class='text-center table-active'>
        <tr>
          <th scope='col'>Project Name</th>
          <th scope='col'>Monday</th>
          <th scope='col'>Tuesday</th>
          <th scope='col'>Wednesday</th>
          <th scope='col'>Thursday</th>
          <th scope='col'>Friday</th>
          <th scope='col'>Saturday</th>
          <th scope='col'>Sunday</th>
        </tr>
      </thead>

{% for week in range(0, n_weeks) %}
  {% for project in projects %}
    {% if project.id == projects[0].id %}
    {% if week%2 == 0 %}<tr class=bg-warning>{% else %}<tr class=bg-light>{% endif %}
      <td></td>
      {% for ii in range(7)  %}
        <td class='text-center'><strong>{{date[ii+week*7]}}</strong></td>
      {% endfor %}
    </tr>
    {% endif %}
  {% endfor %}

  {% for project in projects %}
  {% if week%2 == 0 %}<tr class=bg-warning>{% else %}<tr class=bg-light>{% endif %}
  <th scope='row'>{{project.project_name}}</th>
    {% for ii in range(7)  %}
      {% if date[ii+week*7] >= start_date and date[ii+week*7] <= end_date %}
      <td>{{ form.day(class="form-control", required='required', type='number', step="0.50", value='0')}}</td>
      {% else %}
      <td class='text-center'>xx</td>
      {% endif %}
    {% endfor %}
  </tr>
  {% endfor %}
{% endfor %}

</table>
<div class=form-group>{{ form.submit(class='btn btn-primary') }}</div>


</form>
{% endblock %}

呈现为:

问题是当您查看页面源代码时,您会看到所有表单字段都呈现为:

<td><input class="form-control" id="day" name="day" required="required" step="0.50" type="number" value="0"></td>

我真正需要取回的是输入的值、关联日期和项目名称。例如,如果您在 May-13-2019 投入 3 小时用于 Alpha 项目,您会得到类似 {3, 05-13-2019, 'Alpha'} 的回报,如果您在 May-12-2019 投入 2 小时在 Project Beta 上,您会返回 {2, 05-12-2019, 'Beta}。如果在 POST 上得到 returned,我可以完成剩下的工作。呈现表单时,我可以访问 projectdate 值,但无法弄清楚如何将它们 'attach' 为输入的值。

编辑:我可以添加一些隐藏字段,只是我仍然需要一种方法来识别提交的数据并获取所有表单 ID。 Edit2:我可以给所有表单元素一个唯一的 ID,但我仍然需要一种方法来在提交时获取该 ID

处理表单自定义名称的方法是使用 forms.py:

中的一个函数
def TimeEntryForm(new_name):
    class TempForm(FlaskForm):
        submit = SubmitField('Submit Timesheet')
    setattr(TempForm, new_name, StringField('some_description'))
    return TempForm()

然后在模板中使用类似这样的东西:

{{ form[name](class="form-control", required='required')}}