具有多个变量的render_template [英] render_template with multiple variables

查看:594
本文介绍了具有多个变量的render_template的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask(作为框架)和MongoDB(作为数据库服务器).现在,我所能做的只是传递我从数据库中获得的一个参数:

I am using Flask(as framework) and MongoDB(as database server). Right now, all i can do is just pass one argument that i got from the database:

@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数据库中:我的数据库中有这些东西,我想将它们全部传递给我的模板.

How do i pass multiple variables from the database: eg: in my Mongo database: i have these things in my database and i would like to pass them all to my template.

{
users:'xxx'
content:'xxx'
timestamp:'xxx'
}

是否可以通过使用Flask来做到这一点?

Is it possible to do that by using Flask?

推荐答案

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

You can pass multiple parameters to the view.

您可以传递所有本地变量

You can pass all your local variable

@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文档

这篇关于具有多个变量的render_template的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆