这是我的烧瓶和mongodb snadb.py
中的数据库代码
from flask import Flask, render_template, request, redirect, url_for,request
from bson import ObjectId
from pymongo import MongoClient
import os
app = Flask(__name__)
client = MongoClient("mongodb://127.0.0.1:27017") #host uri
db = client.Student #Select the database
todos = db.Student_list #Select the collection name
def redirect_url():
return url_for('action')
@app.route("/list")
def lists ():
return render_template('snap17.html')
@app.route("/snap17", methods=['GET','POST'])
def action ():
#Adding a Task
name=request.values.get("firstname")
todos.insert({ "name":name})
return redirect("/list")
if __name__ == "__main__":
app.run(debug=True)
`
答案 0 :(得分:0)
在您的视图函数中显示/list
。从数据库读取您的datas
,并相应地将其呈现在您的模板中,如下所示:
@app.route("/list")
def lists():
todolist = db.Student_list.find()
return render_template('snap17.html', todolist=todolist)
然后在模板snap17.html
中将其呈现为
<html>
{% for elem in todolist %}
<li> {{elem['name']}}</li>
{% endfor %}
</html>
要写入数据库,请在您现有的应用程序中发出一个GET
请求: