Amit Pal
Amit Pal

Reputation: 11052

How to access values for a key in a Django template?

Newbie Question: I have a dictionary rendering with extra_Context from a method defined in views.py

My views:

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

If i print the comment the it print like this:

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

I want to pass this dictionary to my template. I tried with this following code:

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

It prints :

first
2
second
2

I want in this way:

first  2
second 2

..and so on

What should i add it to get like above ?

Updated!

 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>

Upvotes: 2

Views: 632

Answers (3)

eusid
eusid

Reputation: 769

"for k(k=key), v(v=value) in object.items"

All that is saying is to iterate over every key value pair i.e. such as name = models.CharField(max_length=50) in object.items. Your view has returned context for object.items which each item is a model instance and has a set of k,v pairs associated with it.

Upvotes: 1

dschulz
dschulz

Reputation: 4786

You don't need that nested for iterating k,v. I just tried this:

View:

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 )

Template:

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

Upvotes: 2

stalk
stalk

Reputation: 12054

Looks like your question just about html markup. Try this:

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

or this:

<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>

Upvotes: 0

Related Questions