带有Flask的mySQL数据库 - 内部服务器错误

时间:2015-07-17 20:54:43

标签: mysql flask sqlalchemy

我正在尝试允许用户将他们的系统保存在我正在创建的Web应用程序上,以便其他用户可以浏览它们,并使用标签对系统进行分类。这是我到目前为止所做的:

app.py

from flask import Flask, request
from flask.ext.sqlalchemy import SQLAlchemy
from contextlib import closing
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost/OFFLsystems'
db = SQLAlchemy(app)

tags = db.Table('tags',
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.tag_id')),
    db.Column('system_id', db.Integer, db.ForeignKey('system.system_id')))

class System(db.Model):
    system_id = db.Column(db.Integer, primary_key=True)
    system_name = db.Column(db.String(80))
    system_description = db.Column(db.String(400))
    tags = db.relationship('Tag', secondary=tags,backref=db.backref('systems', lazy='dynamic'))
    system_date_created = db.Column(db.DateTime)
    system_file = db.Column(db.String(4000))

class Tag(db.Model):
    tag_id = db.Column(db.Integer, primary_key=True)

@app.route("/", methods=['GET','POST'])
def index():
    if request.method == 'POST':
        name = request.form['name']
        desc = request.form['description']
        tags = request.form['tags']
        date = datetime.utcnow()
        sys_file = request.form['file']
        system = System(name,desc,tags,date,sys_file)
        db.session.add(system)
        db.session.commit()
        return render_template("index.html")
    return render_template("index.html")

if __name__ == '__main__':
    app.run()

的index.html

<!DOCTYPE html>
<html>
<head>
    <title>Database Tests</title>
</head>
<body>
<form method="POST">
Name:<input type="text" name="name">
<br>
Description:<input type="textarea" name="description">
<br>
Tags:<input type="text" name="tags">
<br>
File:<input type="text" name="file">
<input type="submit" value="Submit">
</form>
</body>
</html>

问题:我收到内部服务器错误,无法确定这种情况发生的位置。我怀疑它可能与 db.session.add(系统) db.session.commit()行有关,因为我只看到过那些用于命令行,是在程序内部实现它时应该使用的另一个命令吗?

1 个答案:

答案 0 :(得分:0)

system = System(name,desc,tags,date,sys_file)构造函数中,尝试传递关键字参数

system = System(system_name=name, system_description=desc,...)