2017-05-20 2 views
-1

Helo. Ich versuche, alle Kommentare von einem Benutzer und ein Thema, das der Kommentar gehört, aufzulisten, aber ich bekomme doppelte Werte in comment_list.html. Ich denke, ich verschachtelt es nicht richtig.Wie man eine verschachtelte for-Schleife in jinja2 macht, Python 2.7 ohne Doppelergebnisse zu zeigen

Mein Python Hadler folgt:

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) 

Mein jinja2 folgt:

{% 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 %}

Ich würde wirklich Ihre Hilfe zu schätzen wissen. Vielen Dank im Voraus.

Brg

+0

Können Sie hinzufügen, was Ihre tatsächliche Ausgabe aussieht vs. was Sie die erwartete HTML sind sich mit sein ein Beispiel Benutzer, Kommentar und Thema? – alpeware

Antwort

0

Ich habe es geschafft, es zu lösen:

{% 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 %}

Verwandte Themen