我想在页面上多次使用day
上的TimeEntryForm
字段(次数将等于项目数*一个月中的天数)。问题是如何从每个值中返回值以写入数据库。
表单(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日投入3个小时在Project Alpha上,您会得到类似{3,05-13-2019,'Alpha'}的信息,并且如果您在2019年5月12日投入2个小时在Project Alpha上测试版,您可以获得{2,05-12-2019,'Beta}。如果在POST上返回了该信息,我可以做剩下的事情。呈现表单时,我可以访问project
和date
值,但无法弄清楚如何将它们“附加”到输入的值。
编辑:我可以添加一些“隐藏”字段,只是我仍然需要一种方法来标识提交的数据并获取所有表单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')}}