Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于烧瓶中的循环,在html页面中不可见_Python_Flask_Jinja2 - Fatal编程技术网

Python 对于烧瓶中的循环,在html页面中不可见

Python 对于烧瓶中的循环,在html页面中不可见,python,flask,jinja2,Python,Flask,Jinja2,实际上,我正在寻找在flask模板中打印for循环,我使用了不同的方法,但html页面上没有显示任何内容,python代码工作正常,我只是不知道如何使用jinja实现它 Views.py @app.route('/results', methods=['POST', 'GET']) def results(): keyword = {'keyword': request.args.get('keyword')} # First Method keyword = request.

实际上,我正在寻找在flask模板中打印for循环,我使用了不同的方法,但html页面上没有显示任何内容,python代码工作正常,我只是不知道如何使用jinja实现它

Views.py

@app.route('/results', methods=['POST', 'GET'])
def results():

    keyword = {'keyword': request.args.get('keyword')} # First Method
    keyword = request.form['keyword'] # Second Method

    num_tweets=5

    for tweet in tweepy.Cursor(api.search,q=str(keyword)+
        " -filter:retweets",
        result_type='recent',
        lang="en").items(num_tweets):
        clean = re.sub(r"(?:@\S*|#\S*|http(?=.*://)\S*)", "", tweet.text)
        result = cool.api(clean)
    return render_template('pages/results.html')
Results.html

<body>
<div>

{{ result }}
{{ clean }}

</div>        
</body>

{{result}}
{{clean}}

但这一切都毫无意义

你有一个循环通过一系列的推特。在该循环中,您重复使用值覆盖
结果
清除
变量。因此,在循环结束时,您只需要得到最后一个变量

当然,所有这些都没有区别,因为您甚至没有将这些变量发送到要呈现的模板,所以模板当然是空的

您需要在列表中累积这些值。然后,您需要将列表发送到模板。最后,您需要遍历模板中的列表

results = []
for tweet in ...:
    clean = re.sub(r"(?:@\S*|#\S*|http(?=.*://)\S*)", "", tweet.text)
    result = cool.api(clean)
    results.append((clean, result))
 return render_template('pages/results.html', results=results)


您没有传递任何要渲染的数据

results = list() 
for tweet in tweepy.Cursor(api.search,q=str(keyword)+
    " -filter:retweets",
    result_type='recent',
    lang="en").items(num_tweets):
        clean = re.sub(r"(?:@\S*|#\S*|http(?=.*://)\S*)", "", tweet.text)
        result = cool.api(clean)
        results.append((result, clean)) 
return render_template('pages/results.html', results=results)
您还需要在Jinja2中实现一个循环

{% for result in results %} 
{{ result[0] }}
{{ result[1] }}
{% endfor %} 
{% for result in results %} 
{{ result[0] }}
{{ result[1] }}
{% endfor %}