【问题标题】:Use same WTForms field multiple times on a page in Flask在 Flask 的页面上多次使用相同的 WTForms 字段
【发布时间】:2019-10-04 16:47:44
【问题描述】:

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

表单(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>

我真正需要返回的是输入的值、关联的日期和项目名称。例如,如果您在 2019 年 5 月 13 日在 Project Alpha 上投入 3 小时,您会得到类似 {3, 05-13-2019, 'Alpha'} 的信息,如果您在 2019 年 5 月 12 日在 Project 上投入 2 小时您获得的 Beta {2, 05-12-2019, 'Beta}。如果在 POST 上返回,我可以做剩下的事情。呈现表单时,我可以访问 projectdate 值,但不知道如何将它们“附加”到输入的值。

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

【问题讨论】:

    标签: jinja2 flask-wtforms wtforms


    【解决方案1】:

    处理给表单自定义名称的方法是使用 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')}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多