我正在使用Flask构建Web的api 我已经有一个数据库,并且GET请求有效,但是当我尝试POST时,它给了我这个错误
500内部服务器错误
这是我写的代码
#User_schema
class UserSchema(ma.Schema):
class Mata:
fields = ('id', 'name', 'Email', 'Pass', 'Phone', 'Department', 'Major')
#init_schema
user_schema = UserSchema()
users_schema = UserSchema(many=True)
#create user
@app.route('/user', methods=['POST'])
def add_User():
#id = request.json['id']
name = request.json['name']
Email = request.json['Email']
Pass = request.json['Pass']
#Phone = request.json['Phone']
Department = request.json['Department']
Major = request.json['Major']
new_user = User(name, Email, Pass, Department, Major)
db.session.add(new_user)
db.session.commit()
return user_schema.jsonify(new_user)
这是我在邮递员身上添加的请求
{
"name": "User 1",
"Email": "User1@.com",
"Pass": "qq1",
"Phone": "0551",
"Department": "IT 1",
"Major": "IT 1"
}
我也在使用本地服务器 我查看了其他链接,每个人都在询问日志,如果我没记错的话,我想我可以从中获取日志
查看>开发人员>显示DevTools>控制台
这是日志的屏幕截图:(我认为)
这里到底是什么问题我不明白
编辑:这是完整的代码:
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.db'
app.config['SQLALCHEMY_TRACK_MODIFICATION'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
class User (db.Model):
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(30))
Email = db.Column(db.String(30))
Pass = db.Column(db.String(30))
Phone = db.Column(db.Integer)
Department = db.Column(db.String(30))
Major = db.Column(db.String(30))
meeting = db.relationship('Meeting', backref='creator')
meetingroom = db.relationship('MeetingRoom', backref='reserver')
def _init_(self, name, Email, Pass, Phone, Department, Major):
self.name = name
self.Email = Email
self.Pass = Pass
self.Phone = Phone
self.Department = Department
self.Major = Major
class MeetingRoom (db.Model):
mrid = db.Column(db.Integer,primary_key=True)
reserver_id = db.Column(db.Integer, db.ForeignKey('user.id'))
#meetingm = db.relationship('Meeting', backref='roomno')
mid = db.Column(db.Integer, db.ForeignKey('meeting.Mid'))
class Meeting (db.Model):
Mid = db.Column(db.Integer,primary_key=True)
Mname = db.Column(db.String(100))
Des = db.Column(db.String(100))
Date = db.Column(db.String(20))
creator_id = db.Column(db.Integer, db.ForeignKey('user.id'))
meetingroomm = db.relationship('MeetingRoom', backref='mroom')
#roomno_id = db.Column(db.Integer, db.ForeignKey('meetingroom.mrid')
##json
@app.route('/users', methods=['GET'])
def getuser():
users = User.query.all()
output = []
for user in users:
user_data = {}
user_data['id'] = user.id
user_data['name'] = user.name
user_data['Email'] = user.Email
user_data['Pass'] = user.Pass
user_data['Phone'] = user.Phone
user_data['Department'] = user.Department
user_data['Major'] = user.Major
output.append(user_data)
return jsonify({'users' : output})
#User_schema
class UserSchema(ma.Schema):
class Mata:
fields = ('id', 'name', 'Email', 'Pass', 'Phone', 'Department', 'Major')
#init_schema
user_schema = UserSchema()
users_schema = UserSchema(many=True)
#create user
@app.route('/user', methods=['POST'])
def add_User():
#id = request.json['id']
name = request.json['name']
Email = request.json['Email']
Pass = request.json['Pass']
#Phone = request.json['Phone']
Department = request.json['Department']
Major = request.json['Major']
new_user = User(name, Email, Pass, Department, Major)
db.session.add(new_user)
db.session.commit()
return user_schema.jsonify(new_user)
#meeting_schema
class MeetingSchema(ma.Schema):
class Mata:
fields = ('Mid', 'Mname', 'Des', 'Date', 'creator_id')
#init_schema
meeting_schema = MeetingSchema()
meetings_schema = MeetingSchema(many=True)
#create meeting
@app.route('/meeting', methods=['POST'])
def add_meeting():
Mname = request.json['Mname']
Des = request.json['Des']
Date = request.json['Date']
new_meeting = Meeting(Mname, Des, Date)
db.session.add(new_meeting)
db.session.commit()
return user_schema.jsonify(new_meeting)
if __name__ == '__main__':
app.run(debug=True)
添加def错误后:
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked
[SQL: INSERT INTO user (name, "Email", "Pass", "Phone", "Department", "Major") VALUES (?, ?, ?, ?, ?, ?)]
[parameters: ('User 1', 'User1@.com', 'qq1', '0551', 'IT 1', 'IT 1')]
(Background on this error at: http://sqlalche.me/e/e3q8)
答案 0 :(得分:0)
如控制台中所示,问题是您正在使用构造函数初始化User
对象,但是在User
类中尚未定义任何构造函数。
定义这样的构造函数
class User(db.Model): ...
....
def __init__(self, username, email):
self.username = username
self.email = email
# all your attributes
答案 1 :(得分:0)
该错误可能是由于 Flask 的旧版本造成的,因此请尝试通过升级 Flask 版本来解决。
打开 Anaconda Prompt 并输入
pip install --upgrade Flask
然后重新启动您的 IDE