我对模板引擎很新,需要更聪明的人帮助。尝试使用Flask模板生成搜索结果,遇到了很多痛苦。烧瓶:
@app.route('/*?search', methods=['POST', 'GET'])
if request.method == 'GET':
if request.args.get('q'):
qList = re.sub("[^\w]", " ", request.args.get('q') ).split()
htmlA = """
<div class="results" >
<p>
"""
htmlB = """
</p>
<br></br>
</div>
"""
found = None
for it in qList :
found = htmlA + RESULT_BLAH_BLAH_METHOD( it ) + htmlB
return render_template( 'list_of_found_items.html', found=found )
和html模板部分:
<somewhere in html>
{% block content %}
{{ found }}
{% endblock %}
</somewhere in html>
使用此功能,即使存在并且使用打印到控制台输出检查结果,也不会在页面上显示结果。我错过了什么?谢谢。
答案 0 :(得分:1)
autoescaping阻止在python中直接编写HTML。您的代码可以重写为:
qList = re.sub("[^\w]", " ", request.args.get('q') ).split()
return render_template('list_of_found_items.html', items = map(RESULT_BLAH_BLAH_METHOD, qList))
以下模板
<somewhere in html>
{% block content %}
{% for item in qList %}
<div class="results" >
<p>{{item}}</p>
<br></br>
</div>
{% endfor %}
{% endblock %}
</somewhere in html>