用于循环的Python烧瓶不';不要在网站上打印所有单词

用于循环的Python烧瓶不';不要在网站上打印所有单词,python,flask,Python,Flask,我是python初学者,我想在我的网站上打印这个列表。 我在Python Flask中尝试了这段代码: @app.route('/send') def send(): greetings = [ 'Hello', 'how', 'are ', 'you'] for words in greetings: return render_template('test.html', greetings= words) 我还在我的html文件中写了以下内容: <h

我是python初学者,我想在我的网站上打印这个列表。 我在Python Flask中尝试了这段代码:

@app.route('/send')
def send():
    greetings = [ 'Hello', 'how', 'are ', 'you']

    for words in greetings:
        return render_template('test.html', greetings= words)
我还在我的html文件中写了以下内容:

<h1>{{greetings}}</h1>
{{问候语}

但是为什么它总是只打印“Hello”而不打印列表中的所有单词呢?

如果你想在网站上看到列表作为列表,那就这样做吧

@app.route(“/send”)
def send():
问候语=['你好','你好','你']
返回呈现模板('test.html',greetings=greetings)#['Hello','how','are','you']
如果你想让它成为一个句子,那么它就是

@app.route(“/send”)
def send():
问候语=['你好','你好','你']
return render_template('test.html',greetings=“”.join(greetings))#你好吗

输出中只有“Hello”的原因是,在
for
循环中有一个
返回
,因此在这种情况下,在循环的第一次迭代后,模板将被呈现

要呈现所有单词,请将
问候语
列表传递给模板:

@app.route('/send')
def send():
    greetings = [ 'Hello', 'how', 'are ', 'you']
    return render_template('test.html', greetings=greetings)

您应该在模板中进行循环,而不是在路线控制器上

@app.route(“/send”)
def send():
问候语=['你好','你好','你']
返回呈现模板('test.html',问候语=单词)
{%表示问候语中的单词%}
{{word}}
{%endfor%}