具有多个变量的render_template
发布于 2021-01-29 17:04:52
我正在使用Flask(作为框架)和MongoDB(作为数据库服务器)。现在,我所能做的只是传递我从数据库中获得的一个参数:
@app.route('/im/', methods=['GET', 'POST'])
def im_research(user=None):
error = None
if request.method == 'POST':
if request.form['user']:
user = mongo.db.Users.find_one_or_404({'ticker':request.form['user']})
return redirect(url_for('im_user',user= user) )
else:
flash('Enter a different user')
return redirect(url_for('im'))
if request.method == 'GET':
return render_template('im.html', user= None)
我如何从数据库中传递多个变量:例如:在我的Mongo数据库中:我的数据库中有这些东西,我想将它们全部传递给模板。
{
users:'xxx'
content:'xxx'
timestamp:'xxx'
}
使用Flask是否可以做到这一点?
关注者
0
被浏览
52
1 个回答
-
您可以将多个参数传递给视图。
您可以传递所有本地变量
@app.route('/') def index(): content = """ teste """ user = "Hero" return render_template('index.html', **locals())
或只是传递您的数据
def index() : return render_template('index.html', obj = "object", data = "a223jsd" );