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 Flask with SQLAlchemy Basics Connecting to a Database with SQLAlchemy Working with a Plant Database

[Solved] My solution works locally but isn't accepted

My app.py file looks like this:

from models import db, Plant, app
from flask import render_template, url_for, request, redirect


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/form', methods=['GET', 'POST'])
def form():
    if request.form:
        new_plant = Plant(
            plant_type=request.form["type"],
            plant_status=request.form["status"]
        )
        db.session.add(new_plant)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('form.html')


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

I've set up this project locally and it runs perfect, but the quiz won't accept my answer. The feedback I get is to use db.add(). But in the previous video we learned to use db.session.add(). Switching to db.add() instead of db.session.add() also doesn't work.

Also: There's a typo in the form.html file. Line 4:

<form action="{{ url_form('form') }}" method="POST">

should be:

<form action="{{ url_for('form') }}" method="POST">

1 Answer

Steven Parker
Steven Parker
231,236 Points

Switching to db.add will work, but you also need to accommodate a specific formatting quirk of the challenge. It apparently wants you to place the closing parenthesis for Plant on the same line as the 2nd argument, like this:

        new_plant = Plant(
            plant_type=request.form["type"],
            plant_status=request.form["status"] )   # <-- note closing parenthesis
        db.add(new_plant)
        db.commit()

Your formatting is technically fine and looks much nicer as well! :+1:

You may want to report this, along with the typo you found, directly to the Support team. It should get you the "special Exterminator badge". :beetle:

Thanks Steven! Your code works. I'll report the issue to Treehouse