我在通过flask写入string格式时一切正常,但是当我把某列数据改成boolean后,会返回internal server error的错误。
正常情况app.py代码(定义数据库)
class Contacts(db.Model):
id = db.Column(db.Integer, primary_key = True)
company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable = False)
name = db.Column(db.String, nullable = False)
phone = db.Column(db.String)
email = db.Column(db.String)
wechat = db.Column(db.String)
note = db.Column(db.String)
latest_date = db.Column(db.String)
important = db.Column(db.String)
logs = db.relationship('Con_Log', backref = 'contact')
def __repr__(self):
return '<Name %r>' %self.id
正常情况app.py代码(联系人页面)
@app.route('/contacts', methods = ['POST','GET'])
def contacts():
if request.method == "POST":
company_id = request.form['company_id']
name = request.form['name']
phone = request.form['phone']
email = request.form['email']
wechat = request.form['wechat']
note = request.form['note']
latest_date = request.form['latest_date']
important = request.form['important']
new_contact = Contacts(
company_id = company_id,
name = name,
phone = phone,
email = email,
wechat = wechat,
note = note,
latest_date = latest_date,
important = important)
try:
db.session.add(new_contact)
db.session.commit()
return redirect('/contacts')
except:
pass
else:
contacts = Contacts.query.order_by(desc(Contacts.id))
companies = Companies.query
return render_template("contacts.html",
contacts = contacts,
companies = companies )
正常情况的html代码
<div class="container">
<h2>添加新联系人</h2>
<br/>
<form action="/contacts", METHOD="POST">
<select class="form-select" aria-label="Default select example" name='company_id'>
{% for company in companies %}
<option value="{{ company.id }}">{{ company.company_full_name }}</option>
{% endfor %}
</select>
<input type="text" placeholder="请输入联系人名称" name="name" required ="1">
<input type="text" placeholder="请输入联系人电话" name="phone">
<input type="text" placeholder="请输入联系人邮箱" name="email">
<input type="text" placeholder="请输入联系人微信" name="wechat">
<input type="text" placeholder="请输入联系人备注" name="note">
<input type="text" placeholder="请输入最近维护日期(YYYY-MM-DD" name="latest_date" required pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}" title="YYYY-MM-DD"/>
<select class="form-select" aria-label="Default select example" name="important">
<option value="False">否</option>
<option value="True">是</option>
</select>
<input type="submit" value="提交" class="btn btn-secondary">
</form>
</div></br>
出问题的app.py(定义数据库)注意看important那行, 联系人页面和html没动。
class Contacts(db.Model):
id = db.Column(db.Integer, primary_key = True)
company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable = False)
name = db.Column(db.String, nullable = False)
phone = db.Column(db.String)
email = db.Column(db.String)
wechat = db.Column(db.String)
note = db.Column(db.String)
latest_date = db.Column(db.String)
important = db.Column(db.Boolean)
logs = db.relationship('Con_Log', backref = 'contact')
def __repr__(self):
return '<Name %r>' %self.id