Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Looping a form within a route (flask)

HI. I created a flask app with two forms: ProjectForm, StepForm. The ProjectForm simply asks for a title a description and a number of steps. Then the StepForm should be rendered on successive pages for as many times ass stated in the ProjectForm.

What I got so far though arrives upto the first StepForm and allows me to submit it, but it will then render the first StepForm again forever:

@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)