2

初心者の質問: 私は、extra_Contextviews.py で定義されたメソッドからレンダリングする辞書を持っています。

私の見解:

 extra_context = {
    'comment': comment
    }
return direct_to_template(request, 'events/comment_detail.html', extra_context)

を印刷するとcomment、次のように印刷されます。

[{'comment': u'first', 'user': 2}, {'comment': u'second', 'user': 2}]

この辞書をテンプレートに渡したいです。私はこの次のコードで試しました:

       <tbody>
            {% for obj in comment %}
                {% for key,val in obj.items %}
             <tr class="{% cycle 'odd' 'even' %}">
                <td> {{val}}</td>
            </tr>
                {% endfor %}
            {% endfor %}
       </tbody>

それは印刷します:

first
2
second
2

私はこのようにしたい:

first  2
second 2

..等々

上記のようにするには何を追加すればよいですか?

更新しました!

 def comment_detail(request, object_id):
     comment_obj = EventComment.objects.filter(event = object_id)
     comment = comment_obj.values('comment','user')
     extra_context = {
         'comment': comment
         }
     return direct_to_template(request, 'events/comment_detail.html', extra_context)

comment_detail.html

<form action="" method="POST">
<table>
    <thead>
        <tr><th>{% trans "Comments" %}</th><th>{% trans "Timestamp "%}<th>{% trans "User" %}</th></tr>
    </thead>
    <tbody>
        {% if comments %}
    {% for com in comment %}
                <td> {{com.comment}}</enter code heretd>
                <td> {{com.user}}</td>
    {% endfor %}
    {% else %}
     <td> No comments </td>
     {% endif %}
    </tr>  
    </tbody>
</table>
 </form>
4

3 に答える 3

2

forネストされたiteratingは必要ありませんk,v。私はちょうどこれを試しました:

意見:

def testme(request):
   comments = []
   comments.append({'user': 2, 'comment': 'cool story bro'})
   comments.append({'user': 7, 'comment': 'yep. cool story'})

   extra_context = {
      'comments': comments
   }

   return render_to_response('testapp/testme.html', extra_context )

テンプレート:

{% if comments %}
   <b>Comments:</b>
   <ul>
   {% for comment in comments %}
     <li>{{ comment.comment }} (id {{ comment.user }})</li>
   {% endfor %}
   </ul>
{% else %}
   <b>No comments</b>
{% endif %}
于 2012-06-03T15:43:20.890 に答える
0

HTMLマークアップに関するあなたの質問のように見えます。これを試して:

<tbody>
    {% for obj in comment %}
        <tr class="{% cycle 'odd' 'even' %}">
            {% for key,val in obj.items %}          
                <td>{{val}}</td>       
            {% endfor %}
        </tr>
    {% endfor %}
</tbody>

またはこれ:

<tbody>
    {% for obj in comment %}
        <tr class="{% cycle 'odd' 'even' %}"><td>
            {% for key,val in obj.items %}          
                 {{val}}<span> </span>  
            {% endfor %}
        </td>  </tr>
    {% endfor %}
</tbody>
于 2012-06-03T15:06:14.350 に答える