Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access values for a key in a Django template?
    primarykey
    data
    text
    <p>Newbie Question: I have a dictionary rendering with <code>extra_Context</code> from a method defined in views.py</p> <p>My views:</p> <pre><code> extra_context = { 'comment': comment } return direct_to_template(request, 'events/comment_detail.html', extra_context) </code></pre> <p>If i print the <code>comment</code> the it print like this:</p> <pre><code>[{'comment': u'first', 'user': 2}, {'comment': u'second', 'user': 2}] </code></pre> <p>I want to pass this dictionary to my template. I tried with this following code:</p> <pre><code> &lt;tbody&gt; {% for obj in comment %} {% for key,val in obj.items %} &lt;tr class="{% cycle 'odd' 'even' %}"&gt; &lt;td&gt; {{val}}&lt;/td&gt; &lt;/tr&gt; {% endfor %} {% endfor %} &lt;/tbody&gt; </code></pre> <p>It prints :</p> <pre><code>first 2 second 2 </code></pre> <p>I want in this way:</p> <pre><code>first 2 second 2 </code></pre> <p>..and so on</p> <p>What should i add it to get like above ?</p> <p><b> Updated! </b></p> <pre><code> 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) </code></pre> <p>comment_detail.html</p> <pre><code>&lt;form action="" method="POST"&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt;&lt;th&gt;{% trans "Comments" %}&lt;/th&gt;&lt;th&gt;{% trans "Timestamp "%}&lt;th&gt;{% trans "User" %}&lt;/th&gt;&lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; {% if comments %} {% for com in comment %} &lt;td&gt; {{com.comment}}&lt;/enter code heretd&gt; &lt;td&gt; {{com.user}}&lt;/td&gt; {% endfor %} {% else %} &lt;td&gt; No comments &lt;/td&gt; {% endif %} &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload