我需要将所有数据从 SQL 表传输到 html 页面。在 SQLAlchemy 中我会做这样的事情:
class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
first = db.Column(db.String(80))
last = db.Column(db.String(80))
@app.route('/authors')
def get_authors():
authors = Author.query.all()
# Serialize the queryset
result = authors_schema.dump(authors)
return jsonify({'authors': result.data})
Run Code Online (Sandbox Code Playgroud)
有没有类似authors = Author.query.all()
peewee的东西?
我不知道如何保存数据服务器端。这是我的路线:
@app.route('/results', methods=['GET', 'POST'])
@login_required
def results():
if request.method == 'POST':
results = request.form
return json.dumps(request.form)
Run Code Online (Sandbox Code Playgroud)
在那里我将请求表从字典转换为字符串。这是构成表单的 HTML:
<form action = "/results" method = "POST">
<input type="text" placeholder="Host" name="host" value="host">
<input type="text" placeholder="Longname" name="longname" value="longname">
<input type="text" placeholder="Shortname" name="shortname" value="shortname">
<p><input class="btn btn-primary" type="submit" value="send"></p>
Run Code Online (Sandbox Code Playgroud)
我做了一个结果页面,看看我是否可以将表单转换为 JSON。但是现在我卡住了,我不知道如何将结果保存到文件中。每次我发布表单时,我都会返回如下所示的内容:
{hosts ["host": "a", "shortname": "c", "longname": "b"], ["host": "a", "shortname": "c", "longname": "b"]}
Run Code Online (Sandbox Code Playgroud)