具有多个变量的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 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您可以将多个参数传递给视图。

    您可以传递所有本地变量

    @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" );
    

    API文档



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看