After completing the excellent flask tutorial, I wanted to add a second class to my database. My problem is that when I enter a comment into the new database I get a 400 Bad Request error. My flask_app.py looks like this:
from flask import Flask, redirect, render_template, request, url_for
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["DEBUG"] = True
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username="jss367",
password="mysqldbpw0o0o",
hostname="jss367.mysql.pythonanywhere-services.com",
databasename="jss367$comments",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
db = SQLAlchemy(app)
class Comment(db.Model):
__tablename__ = "comments"
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(4096))
class Feature(db.Model):
__tablename__ = "features"
id = db.Column(db.Integer, primary_key=True)
feature_content = db.Column(db.String(250))
@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()
feature = Feature(feature_contents=request.form["feature_contents"])
db.session.add(feature)
db.session.commit()
return redirect(url_for('index'))
My main_page.html has two text boxes in it. The first is for the comments, which is working, and the second is for features. Here is the relevant part:
<body>
<div class="container">
{% for comment in comments %}
<div class="row">
{{ comment.content }}
</div>
{% endfor %}
<div class="row">
<form action="." method="POST">
<textarea class="form-control" name="contents" placeholder="Enter a comment"></textarea>
<input type="submit" value="Post comment">
</form>
</div>
<div class="row">
<form action="." method="POST">
<textarea class="form-control" name="feature_contents" placeholder="Enter your feature requests here!"></textarea>
<input type="submit" value="Send request">
</form>
</div>
</div>
</body>
I see the database from mysql and everything looks fine to me. Here's the output from that:
mysql> show tables;
+---------------------------+
| Tables_in_jss367$comments |
+---------------------------+
| comments |
| features |
+---------------------------+
mysql> describe features;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| feature_content | varchar(250) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Does anyone have any suggestions for what to try next? Thanks!