So for the last week I have tried and failed to add data into different tables on the same database. Playing with the code today I noticed the error may not be what it seems. My code below has two different classes, used as my tables, and two different pages. It is supposed to have two different pages that will add data into two different tables, simple. I am sure that it is incorrect but my actual problem is that the code is unresponsive. For example, the page '/test' will and always has, added information into the table "comments" which corresponds to the '/' page (main_page) as I changed the code in '/test' to begin to get a trial and error sense of how it works it made no change, now I realize, I can comment out the entire add and commit process yet it still will add the information into the database and redirect the url to 'confirm' instead of 'test' Clearly the '/test' page is running the code from the main_page but why would this be? The only code I have not provided is connecting to MySQL which was the same method as in Beginners Guide. and the html which is the same as in the guide on both pages. Both tables are setup and have data from the SQL console.
class Comment(db.Model):
__tablename__ = "comments"
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(4096))
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80))
@app.route("/test", methods=["GET", "POST"])
def test():
if request.method == "GET":
return render_template("test.html", users=User.query.all())
#user = User(username=request.form["contents"])
#User.session.add(user)
#User.session.commit()
return redirect(url_for('test'))
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
return render_template("main_page.html", comments=Comment.query.all())
comment = Comment(content=request.form["contents"])
db.session.add(comment)
db.session.commit()
return redirect(url_for('confirm'))