Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

python - render_template with multiple variables

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)

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'
}

Is it possible to do that by using Flask?

question from:https://stackoverflow.com/questions/12096522/render-template-with-multiple-variables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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())

or just pass your data

def index() :
    return render_template('index.html', obj = "object", data = "a223jsd" );

api doc


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...