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 trialDanielle Murray
Python Development Techdegree Graduate 7,755 PointsPlease could you help me solve this challenge. I am getting an error message about my function not being inside the if
Please could you help me solve this challenge. I am getting an error message about my function new_plant not being inside the if statement. I've checked maybe it is my indentation but I've changed it a few times. My challenge won't pass and I'm struggling understand why. Please could you help me.
from models import db, Plant, app
from flask import render_template, url_for, request, redirect
@app.route('/')
def index()
render_template plants = Plant.query.all()
return render_template(plants=plants)
@app.route('/form', methods=['GET', 'POST'])
def form():
if 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)
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())
{% 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 %}
{% extends 'layout.html' %}
{% block content %}
<h1>Our Plants:</h1>
<div>
<h2>Plant Type</h2>
<p>Plant Status</p>
</div>
{% endblock %}
3 Answers
jb30
44,807 PointsInstead of checking the truthiness of form
, try checking request.form
.
@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 form:
and return render_template('form.html',)
should be at the same indentation level as each other.
Danielle Murray
Python Development Techdegree Graduate 7,755 Pointsjb30 from models import db, Plant, app from flask import render_template, url_for, request, redirect
@app.route('/') def index() render_template plants = Plant.query.all() return render_template(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)
This is my code after I made the changes you suggested and it seems to still be giving errors even though the 4 lines after the if statement are indented on the same line.
jb30
44,807 PointsTry removing the space after Plant
in new_plant = Plant (plant_type=request.form['type'], plant_status=request.form['status'])
.
Danielle Murray
Python Development Techdegree Graduate 7,755 Pointsjb30 thank you so much for your help you were right my space was the problem
Danielle Murray
Python Development Techdegree Graduate 7,755 PointsDanielle Murray
Python Development Techdegree Graduate 7,755 PointsHi jb30 thank you so much but I've used request.form and I've put if request.form and return render_template on the same indentation level like you suggested but I'm still getting an error message - - Bummer: Make sure your new_plant variable is inside of the form function's if statement.
jb30
44,807 Pointsjb30
44,807 PointsAre the four lines after the if statement at the same indentation as each other? Do they each start with four more spaces than the line containing the if statement?