循环使用Jinja2模板中的列表 [英] Looping over a list in a Jinja2 template
问题描述
问题I现在是我想在我的HTML文件中显示评论。要做的是我想在我的for循环中调用一个名为show_comments的python函数在我的html文件中。 python函数如下所示:
@ app.route('/ comments /< entryid>)
def show_comments(entryid):
db = get_db()
curId = db.execute('select id,comment from comments where entry_id = entryid order by id desc')
comments = [dict ($)
return render_template('show_entries.html',comments = comments)
$ c $($ id = row [0],comment = row [1])用于curld.fetchall我的模板如下所示: %扩展layout.html%}
{%block body%}
{%if session.logged_in%}
< dl>
< dt>标题:
< dd>< input type = text size = 30 name = title>
< dt>文本:
< dd>< textarea name = text rows = 5 cols = 40>< / textarea>
< dd>< input type = submit value = Share>
< / dl>
< / form>
{%endif%}
< ul class = entries>
{%用于输入条目%}
< li>< h2> {{entry.title}}< / h2> {{entry.text}}
{{url_for ('show_comments',entryid = entry.id)}}
< ul class = comments>
{%用于评论中的评论%}
< li> {{acomment.comment}}
< / li>
< / br>
< / ul>
{%endfor%}
{%if session.logged_in%}
< dl>
< dt>评论:
< dd>< textarea name = comment rows = 2 cols = 40>< / textarea>
< dd><输入类型=提交值=评论>
< / dl>
< / form>
{%endif%}
{%else%}
< li>< em>令人难以置信。目前没有条目< / em>
{%endfor%}
< / ul>
{%endblock%}
解决方案没有真正告诉我们你的问题是什么,所以我不知道这是否会有所帮助。但是我会在这里指出三个具体的错误:
第一个是你将你的注释放在一个无序列表中(< ul>
标签),但是您将结束标记(< / ul>
)放在循环中而不是放在循环之外。
< / br>
应写成< br />
,但实际上这个标签并不属于那里。
第二,你的HTML坏掉了。 HTML属性(标签中的部分像 type = submit
)应该在值周围有引号。例如,它应该看起来像 type =submit
。大多数浏览器对这样的事情都是宽容的,但最好不要指望它,并写出正确的HTML。
Im trying to make a simple blog website with the Flask framework. Each entry in my blog has a title, text and comments. the title and text are stored in a table named entries and the comments in a table named comments that links the comments to the corresponding entry with a foreign key.
The problem I have now is that I want to display the comments in my html file. To do is I want to call a python function named show_comments in my html file while I'm in a for loop. The python function looks like this:
@app.route('/comments/<entryid>')
def show_comments(entryid):
db = get_db()
curId = db.execute('select id, comment from comments where entry_id=entryid order by id desc')
comments = [dict(id=row[0], comment=row[1]) for row in curId.fetchall()]
return render_template('show_entries.html', comments=comments)
My template looks like this:
% extends "layout.html" %}
{% block body %}
{% if session.logged_in %}
<form action="{{ url_for('add_entry') }}" method=post class=add-entry>
<dl>
<dt>Title:
<dd><input type=text size=30 name=title>
<dt>Text:
<dd><textarea name=text rows=5 cols=40></textarea>
<dd><input type=submit value=Share>
</dl>
</form>
{% endif %}
<ul class=entries>
{% for entry in entries %}
<li><h2>{{ entry.title }}</h2>{{ entry.text }}
{{ url_for('show_comments', entryid=entry.id) }}
<ul class=comments>
{% for acomment in comments %}
<li>{{ acomment.comment }}
</li>
</br>
</ul>
{% endfor %}
{% if session.logged_in %}
<form action="{{ url_for('add_comment', key=entry.id) }}" method=post class=add-entry>
<dl>
<dt>Comment:
<dd><textarea name=comment rows=2 cols=40></textarea>
<dd><input type=submit value=Comment>
</dl>
</form>
{% endif %}
{% else %}
<li><em>Unbelievable. No entries here so far</em>
{% endfor %}
</ul>
{% endblock %}
解决方案 You haven't really told us what your problem is so I don't know if this will help. But I'll point out three specific mistakes here:
The first is that you're putting your comments in an unordered list (the <ul>
tag) but you put the end tag (</ul>
) inside the loop instead of outside it.
</br>
should be written <br />
, but really that tag doesn't belong there in the first place.
Second, your HTML is broken. HTML attributes (the parts in tags like type=submit
) should have quotation marks around the values. For example, it should look like type="submit"
. Most browsers are forgiving when it comes to things like that, but it's better not to count on that and write correct HTML.
这篇关于循环使用Jinja2模板中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!