根据其他表格的值循环重复烧瓶形式

时间:2018-07-22 00:32:55

标签: python flask flask-wtforms peewee

我正在尝试使用表单(ProjectForm)中的值(n)循环生成n次第二个表单(StepForm)。 ProjectForm可以工作,创建项目并 然后将我发送到StepForm,但是提交后,StepForm不会更新,循环永远不会停止。

@app.route("/new_project", methods=("POST", "GET"))
@login_required
def post_project():
    form = forms.ProjectForm()
    if form.validate_on_submit():
        models.Project.create(user=g.user.id,
                              title=form.title.data,
                              steps_number = form.steps_number.data,
                              description=form.description.data.strip())
        flash("Project created", "success")
        if form.steps_number.data != 0:
            project = models.Project.select().where(models.Project.title == form.title.data).get()
            return redirect(url_for("add_step",project_id=project.id))
        if form.steps_number.data == 0:
            return redirect(url_for("projects"))
    return render_template("post_project.html", form=form)

@app.route("/new_step/<int:project_id>", methods=("POST", "GET"))
@login_required
def add_step(project_id):
    form = forms.StepForm()
    project = models.Project.select().where(models.Project.id == project_id).get()
    steps = 0
    for i in range(0,project.steps_number):
        steps+=1
        if form.validate_on_submit():
            models.Step.create(project=project.id,
                                    title=form.title.data.strip(),
                                    instruction=form.instruction.data.strip())
            flash("Step Added", "success")
            if project.steps_number == steps:
                return redirect(url_for("projects"))
        return render_template("add_step.html", form=form, step=steps)

已解决,请在下方查看答案

@app.route("/new_project", methods=("POST", "GET"))
@login_required
def post_project():
    form = forms.ProjectForm()
    if form.validate_on_submit():
        models.Project.create(user=g.user.id,
                              title=form.title.data,
                              steps_number = form.steps_number.data,
                              description=form.description.data.strip())
        flash("Project created", "success")
        if form.steps_number.data != 0:
            project = models.Project.select().where(models.Project.title == form.title.data).get()
            return redirect(url_for("add_step",project_id=project.id,step_id=1))
        if form.steps_number.data == 0:
            return redirect(url_for("projects"))
    return render_template("post_project.html", form=form)

@app.route("/new_step/<int:project_id>/<int:step_id>", methods=("POST", "GET"))
@login_required
def add_step(project_id,step_id):
    project = models.Project.select().where(models.Project.id == project_id).get()
    form = forms.StepForm()
    step = step_id
    if form.validate_on_submit():
        models.Step.create(project=project.id,
                            title=form.title.data.strip(),
                            instruction=form.instruction.data.strip())
        flash("Step Added", "success")
        step +=1
        return redirect(url_for("add_step",project_id=project.id,step_id=step))
    if project.steps_number == step:
                return redirect(url_for("projects"))
    return render_template("add_step.html", form=form, step_id=step)

1 个答案:

答案 0 :(得分:2)

您不能在循环中使用return并期望该循环能工作

def f():
    for i in range(100):
        return i

f()
# >>> 0

您的页面和路由需要通过响应式操作,因此将当前步骤号作为渲染参数传递给页面: render_template("add_step.html", form=form, step=steps)  然后将该数字传递回添加步骤路径和输入参数,并删除无效的内部循环:

@app.route("/new_step/<int:project_id>/<int:step>", methods=("POST", "GET"))
@login_required
def add_step(project_id, step):

您将需要更新表单操作html链接以包含步骤