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?
redirect returns a 302 header to the browser, with its Location header as the URL for the index function. render_template returns a 200, with the index. html template returned as the content at that URL. Voting is disabled while the site is in read-only mode.
render_template is a Flask function from the flask. templating package. render_template is used to generate output from a template file based on the Jinja2 engine that is found in the application's templates folder. Note that render_template is typically imported directly from the flask package instead of from flask.
Instead of returning hardcode HTML from the function, a HTML file can be rendered by the render_template() function. Flask will try to find the HTML file in the templates folder, in the same folder in which this script is present.
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
return render_template('im.html', user= None, content = xxx, timestamp = xxx)
You can pass as many variables as you need. The api
excerpt:
flask.render_template(template_name_or_list, **context) Renders a template from the template folder with the given context.
Parameters: template_name_or_list – the name of the template to be rendered, or an iterable with template names the first one existing will be rendered context – the variables that should be available in the context of the template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With