0

I have a simple filter where a user enters a string, term, which is compared against the column companyId in my local database. If there is a match, the appropriate record/s are then rendered in a table within my template. However, no data is being rendered in the template, only rows of empty fields matching the number of records for a particular query. I have similar logic used for displaying these records unfiltered which works fine.

Edit:

When I removed the key value and tried to render only the object such as {{ object }}, the following is displayed: (Opportunity: Opportunity object (8)

views.py

def opportunity_dashboard(request):
    try:
        term = request.GET.get('search_query')
        if term:
            filtered_objects = Opportunity.objects.filter(companyId__icontains=term)

filtered_local_zip = zip(filtered_objects)

context = {
    'term': term,
    'filtered_local_zip': filtered_local_zip,
    'filtered_connectwise_zip': filtered_connectwise_zip
}

return render(request, 'website/opportunity_dashboard.html', context)

template.html

{% if term %}
{% for object in filtered_local_zip %}
<tr>
    <th style="text-align: center;">
        <a href="https://solutions.byteworks.com/new_opportunity/new_opportunity_review?id={{ object.id }}">&#9998;</a>
    </th>
        <td>
            <div class="select">
                <select disabled id="bw_status" name="status">
                    <option value="{{ object.status }}">{{ object.status }}</option>
                </select>
            </div>
        </td>
        <td>
            <a{{ object.opportunityName }}</a>
        </td>
        <td>{{ object.companyId }}</td>
        <td>
            <div class="select">
                <select id="bw_account_manager" name="account_manager">
                    <option value="{{ object.accountManager }}">{{ object.accountManager }}</option>
                </select>
            </div>
        </td>
Saturnix
  • 10,130
  • 17
  • 64
  • 120
t0bi
  • 17
  • 5
  • if you are using python3, replace `zip(filtered_objects)` with `list(zip(filtered_objects))` – Yaman Ahlawat Aug 21 '19 at 20:28
  • @YamanAhlawat I am, but have not had any issues so far without list declaration. I made the change and no data is being rendered still. For instance, `{{ object.accountManager }}`, there is no value where there should be one. – t0bi Aug 21 '19 at 20:31
  • `for object in context.filtered_local_zip` You are passing context as a dict. You need to either iterate on dict or change the forloop to iterate on context.filtered_local_zip. – Yaman Ahlawat Aug 21 '19 at 20:38
  • you don't need to use zip. just do `list(filtered_objects)` – Yaman Ahlawat Aug 21 '19 at 20:42
  • @YamanAhlawat please see my edit, apparently I am returning some data, but only a description of the database object and not the actual values of the record – t0bi Aug 21 '19 at 20:43
  • @YamanAhlawat sorry, for the sake of clarity in this problem I have redacted a second database object I am rendering, hence the usage of `zip`. – t0bi Aug 21 '19 at 20:44
  • What happens if you completely remove zip()? Simply pass filtered_objects and iterate over it. – Saturnix Aug 21 '19 at 20:46
  • @Saturnix thats correct. – Yaman Ahlawat Aug 21 '19 at 20:48
  • @t0bi if you are getting object, just access the fields `object.some_field` – Yaman Ahlawat Aug 21 '19 at 20:50
  • @Saturnix I removed `zip` and I was able to iterate over the data, however, I do need to use `zip` as I have more than one object I am rendering and iterating over in congruence. Why would `zip` be giving me issues when iterating? I have the same exact logic used elsewhere except I do not filter the object and it works fine. – t0bi Aug 21 '19 at 20:50
  • if you are using python 3, zip gives an iterator – Yaman Ahlawat Aug 21 '19 at 20:52
  • Then this is a different problem from the one your question asks, which is addressed here: https://stackoverflow.com/questions/2415865/iterating-through-two-lists-in-django-templates – Saturnix Aug 21 '19 at 20:52
  • @Saturnix no, I am able to iterate over two or more lists fine, but when I use a filter on my object, this is when I have issues accessing the key value. So, `{{ object.status }}` does not return a value while it does without the object filter. – t0bi Aug 21 '19 at 20:54
  • Why do you use {{ object.status }}? The link I've posted shows you have to either change the iterator or use {{ object.0.status }} – Saturnix Aug 21 '19 at 21:05
  • @t0bi updated my answer to include a working example – Saturnix Aug 21 '19 at 21:08

1 Answers1

0

You don't need zip() nor list() to iterate over a queryset in a Django template.

Simply pass it to the context.

filtered_local_zip = filtered_objects

and in the template iterate over it like you're already doing:

{% for object in filtered_local_zip %}

If you need to iterate over zip(), read this: https://stackoverflow.com/a/4238261/1307020

Which suggests either this

{% for object0, object1 in filtered_local_zip %}

or this

{{ object.0.property }}

Demonstration: https://repl.it/repls/UntidyLimpingCollaborativesoftware

Saturnix
  • 10,130
  • 17
  • 64
  • 120
  • Please see my reply to your comment, this works, however I am passing four objects to my template for iteration in congruence. Hence the usage of `zip`. If you have any ideas I would very much appreciate them. – t0bi Aug 21 '19 at 20:52
  • 1
    @t0bi pass all queryset separately, create key, value pairs in context. and loop through them individually if you don't want to use zip – Yaman Ahlawat Aug 21 '19 at 20:55