Forums

400 Bad Request for second MYSQL database class

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!

do you see an error traceback in your logs?

I don't see anything in my error log. The only thing I see in my server log is:

2017-01-22 23:18:15 Respawned uWSGI worker 1 (new pid: 3406) 2017-01-22 23:18:15 spawned 2 offload threads for uWSGI worker 1 2017-01-22 23:18:16 announcing my loyalty to the Emperor...

just a quick thing to double check- does the get work vs the post doesn't work?