Afe*_*ziz 30 python mongodb flask
我使用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)
Run Code Online (Sandbox Code Playgroud)
我如何从数据库传递多个变量:例如:在我的Mongo数据库中:我在我的数据库中有这些东西,我想将它们全部传递给我的模板.
{
users:'xxx'
content:'xxx'
timestamp:'xxx'
}
Run Code Online (Sandbox Code Playgroud)
使用Flask可以做到这一点吗?
hea*_*eat 52
您可以将多个参数传递给视图.
您可以传递所有本地变量
@app.route('/')
def index():
content = """
teste
"""
user = "Hero"
return render_template('index.html', **locals())
Run Code Online (Sandbox Code Playgroud)
或者只是传递你的数据
def index() :
return render_template('index.html', obj = "object", data = "a223jsd" );
Run Code Online (Sandbox Code Playgroud)
abh*_*nav 13
return render_template('im.html', user= None, content = xxx, timestamp = xxx)
Run Code Online (Sandbox Code Playgroud)
您可以根据需要传递尽可能多的变量.该API
摘抄:
flask.render_template(template_name_or_list,**context)使用给定的上下文从模板文件夹中呈现模板.
参数:template_name_or_list - 要呈现的模板的名称,或具有模板名称的iterable,第一个现有的将呈现上下文 - 应该在模板的上下文中可用的变量.
PYB*_*PYB 13
还可以将列表传递给render_template的上下文变量,并使用 HTML 中的 Jinja 语法引用其元素。
示例.py
mylist = [user, content, timestamp]
return render_template('exemple.html', mylist=mylist)
Run Code Online (Sandbox Code Playgroud)
示例.html
...
<body>
{% for e in mylist %}
{{e}}
{% endfor %}
</body>
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60250 次 |
| 最近记录: |