Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask with SQLAlchemy Basics Connecting to a Database with SQLAlchemy Working with a Plant Database

Mat Clark
seal-mask
.a{fill-rule:evenodd;}techdegree
Mat Clark
Python Development Techdegree Student 4,369 Points

Trouble with third step of challenge

I just keep getting a "Replace Plant Type with the information from your database" message. I believe I've done this. Am I making an error somewhere?

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


@app.route('/')
def index():
    plants = Plant.query.all()
    return render_template('index.html', plants=plants)


@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.add(new_plant)
        db.commit()
        return redirect(url_for('index'))

    return render_template('form.html')


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)
models.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///plants.db'
db = SQLAlchemy(app)

class Plant(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    plant_type = db.Column(db.String())
    plant_status = db.Column(db.String())
form.html
{% extends 'layout.html' %}

{% block content %}
    <form action="{{ url_form('form') }}" method="POST">
        <label for="type">Plant Type:</label>
        <input type="text" name="type" id="type" placeholder="Plant type">

        <p>Plant Status:</p>
        <label for="good">GOOD</label>
        <input type="radio" name="status" id="good">
        <label for="ok">OK</label>
        <input type="radio" name="status" id="ok">
        <label for="bad">BAD</label>
        <input type="radio" name="status" id="bad">
    </form>
{% endblock %}
index.html
{% extends 'layout.html' %}

{% block content %}
    <h1>Our Plants:</h1>
    {% for plant in plants %}
      <div>
          <h2>{{ plant.type }}</h2>
          <p>{{ plant.status }}</p>
      </div>
    {% endfor %}
{% endblock %}

1 Answer

Mel Rumsey
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Mel Rumsey
Treehouse Staff

Mat Clark You are super close! The column names of type and stats in the model are actually plant_type and plant_stats. So if you change stats to plant_stats and type to plant_type, you will be good to go!