2017-05-20 12 views
-1

Helo。 1人のユーザーからのすべてのコメントとそのコメントが属するトピックを一覧表示しようとしていますが、comment_list.htmlには2つの値があります。 私はそれを正しく入れ子にしていないと思います。jinja2、python 2.7で二重の結果を表示せずにネストされたforループを作る方法

私のPythonのHadler博士は以下の通りです:

class CommentsListHandler(BaseHandler): 
def get(self): 
    user = users.get_current_user() 
    comments = Comment.query(Comment.deleted==False, Comment.author_email==user.email()).fetch() 
    topics = Topic.query(Topic.deleted==False).fetch() 
    params = {"comments": comments, "topics": topics} 
    return self.render_template("comments_list.html", params=params) 

マイJinja2のは以下の通りです:

{% for comment in comments|sort(attribute='created') %} 
 
    {% for topic in topics %} 
 
<div class="panel panel-warning"> 
 
     <div class="panel-heading">{{ comment.author_email }} on {{ comment.created.strftime("%d.%m.%Y at %H:%M") }} 
 
      in topic: <a href="/topic-details/{{topic.key.id()}}"> {{ topic.title }} </a></div> 
 
     <div class="panel-body"> 
 
      <p>{{ comment.content }}</p> 
 
     </div> 
 
</div> 
 

 
    {% endfor %} 
 
{% endfor %}

は、私は本当にあなたの助けをいただければ幸いです。 ありがとうございます。

Brgの

+0

あなたはあなたの実際の出力は、あなたが期待されるHTMLは、一緒にいたいと思いますどのような対どのように見えるかを追加することができますサンプルユーザー、コメント、トピック – alpeware

答えて

0

私はそれを解決するために管理している:

{% for comment in comments|sort(attribute='created') %} 
 
    {% for topic in topics %} 
 
     {% if topic.key.id() == comment.topic_id %} 
 
<div class="panel panel-warning"> 
 
     <div class="panel-heading">{{ comment.author_email }} on {{ comment.created.strftime("%d.%m.%Y at %H:%M") }} 
 
      in topic: <a href="/topic-details/{{topic.key.id()}}"> {{ topic.title }} </a></div> 
 
     <div class="panel-body"> 
 
      <p>{{ comment.content }}</p> 
 
     </div> 
 
</div> 
 
     {% endif %} 
 
    {% endfor %} 
 
{% endfor %}

関連する問題