这是我在flask和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)
`
在你的qazxsw poi的视图功能中。从数据库中读取你的qazxsw poi,然后在你的模板中渲染它,如下所示:
/list
在你的模板中,datas
将其渲染为
@app.route("/list")
def lists():
todolist = db.Student_list.find()
return render_template('snap17.html', todolist=todolist)
要写入您的数据库,请在现有的apllication中生成这样的snap17.html
请求:
<html>
{% for elem in todolist %}
<li> {{elem['name']}}</li>
{% endfor %}
</html>