在我的网站上,我有一个包含许多活动的模型实验。我有一个视图,人们可以在实验中添加或删除活动。
我展示了一个包含此实验活动一部分的表格,以及一个不属于本实验的活动表格。用户可以检查他们要添加或删除的活动,并使用表格下的提交按钮。
但是,当我更新一个表单上的选项列表时,另一个表单上的选项列表会反映出这一点。我做错了吗?
对于向/从实验中添加和删除活动,我使用相同的表单。
class MultiCheckboxField(SelectMultipleField):
widget = ListWidget(prefix_label=False)
option_widget = CheckboxInput()
class ActivityListForm(Form):
activities = MultiCheckboxField(validators=[DataRequired()], choices=[])
submit = SubmitField("Submit")
def populate_activities(self, activities_set):
activities_mapping = {}
for activity in activities_set:
activities_mapping[str(activity.id)] = activity
choice_tuple = (str(activity.id), activity.question)
self.activities.choices.append(choice_tuple)
return activities_mapping
以下是我的观点:
def settings_experiment(exp_id):
experiment = Experiment.query.get(exp_id)
remove_activities_form = ActivityListForm(prefix="remove")
add_activities_form = ActivityListForm(prefix="add")
remove_activities_mapping = remove_activities_form.populate_activities(
experiment.activities)
add_activities_mapping = add_activities_form.populate_activities(
Activity.query.\
filter(not_(Activity.experiments.any(id=experiment.id))).all())
return render_template("experiments/settings_experiment.html",
experiment=experiment,
update_experiment_form=update_experiment_form,
remove_activities_form=remove_activities_form,
add_activities_form=add_activities_form,
add_activities_mapping=add_activities_mapping,
remove_activities_mapping=remove_activities_mapping)
虽然remove_activities_form
和add_activities_form
的选项分别设置为不同的列表,但它们最终都包含两个选项列表的联合,这会混淆我的模板渲染。有没有办法让它们分开或者我搞砸了?