Reputation: 740
I am trying to pass the data to the template but it doesn't show up
<div id="table">
<table id="table_id">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
{% for key, value in users.items %}
<tr>
<td>{{ key }}{{ value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
and this is the view
@login_required
def event_detail(request, pk):
messages = Chat.objects.filter(room=pk)
users = Chat.objects.filter(room=pk).values('user__username').distinct()
event_users = Event.objects.filter(id=pk)
response_data = {
'messages': messages,
'pk': pk,
'users': users
}
return render(request, 'chat/event_detail.html', response_data)
My table doesn't show data.
Upvotes: 0
Views: 32
Reputation: 6414
You will send a queryset in template. You can try this
{% for value in users %}
<tr>
<td>{{ value.user__username }}</td>
</tr>
{% endfor %}
Upvotes: 0
Reputation: 13731
users
isn't a dict, it's a QuerySet.
{% for user_values in users %}
<tr>
{% for key, value in user_values.items %}
<td>{{ key }}: {{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
Edit:
I changed it to support your usage of .values()
. However, unless you have a great reason to do so, I'd recommend using the ORM, .select_related('user')
and model instances rather than .values()
. Simplicity can outweigh performance.
Edit 2:
If you want a list of usernames you can do this:
usernames = Chat.objects.filter(room=pk).values_list('user__username', flat=True).distinct()
That will be much cleaner and still do what you originally wanted. Just name the variable to indicate it's only the usernames.
Upvotes: 1