almost a beginner almost a beginner - 3 months ago 17
HTML Question

Passing Django variable to `<div>` tag

I want to pass a Django variable to a tag. In my case I would like to pass it to the

<div>
tag.

I want to set the
<div>
tag id with the variables returned by my view.

For example (template):

<div class="pool">
{% for pools in poolID %}
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#{{ pools }}">
{{ pools }}
</button>
<div id="{{ pools }}" class="collapse">
<div>
{% for mem in member %}
{{ mem.first_name }}

<br>
{% endfor %}
</div>
</div>

{% endfor %}
</div>


Although I don't think the problem is in the view, then again:

def pool(request):
poolID = Member.objects.order_by().values_list('pool_id').distinct()
member = Member.objects.all()
return render(request, 'app/pool.html', {'poolID':poolID, 'member':member})


When I click the button, it should expand the div, but it doesn't. Meaning the variable syntax is wrong (that's my assumption at least).

The first variable that my view returns is: ('test pool id',)

It should be assigned to target of the button (which includes the hash before the variable) and the id of the div.

Any help or direction would be appreciated,

Thanks

Answer

values_list returns a list of lists, even if you're only selecting a single field per item. You should use flat=True to get a single list.

poolID = Member.objects.order_by().values_list('pool_id', flat=True).distinct()
Comments