I am learning Django and building an inventory app for a laboratory. I already have data in all my models, and now I want to search the database based on some criteria and display the results in a table. First I ask the user to input the search terms (that part works), then I query the db (that also works) but when it's time to display results all I get is an empty template. No error messages.
These are my views:
def choose_filter_primers(request): # this works fine
if request.method == "GET":
radiobtn_form = PrimerRadiobtn(request.GET)
if radiobtn_form.is_valid():
# get value from user input and store it in request.session dict
request.session['filter_by'] = radiobtn_form.cleaned_data['CHOOSE_FIELD']
# go to the next step in the search form
return render(request, 'lab_inventory/filter_primers.html')
else:
radiobtn_form = PrimerRadiobtn()
return render(request, 'lab_inventory/choose_filter_primers.html', {'radiobtn_form': radiobtn_form})
def filter_primers(request): # this works fine
# get filter field from views.choose_filter_primers
filter_by = request.session.get('filter_by')
if request.method == "POST":
form = FilterPrimerForm(request.POST)# or None)
if form.is_valid():
# get value from user input and store it in request.session dict
request.session['contains'] = form.cleaned_data.get("contains")
# go to the next step in the search form
return render(request, 'lab_inventory/search_results_primers.html')
else:
return render(request, 'lab_inventory/choose_filter_primers.html')
else:
form = FilterPrimerForm(request.POST)
context = {'form': form}
return render(request, 'lab_inventory/filter_primers.html', context)
def search_results_primers(request): # MY PROBLEM IS HERE SOMEWHERE
search_term = request.GET['contains'] # this variable refers to the value 'Ha'
filter_by = request.session.get('filter_by') # this variable refers to the value 'primer_name'
if filter_by == 'primer_name':
query = Primer.objects.filter(primer_name__contains=search_term).values()
result = {'query': query}
return render(request, 'lab_inventory/search_results_primers.html', result)
The query in search_results_primers
returns this (from running it in the shell):
<QuerySet [{'id': 303, 'primer_name': 'Ha-9-F', 'primer_seq': '5-TAGCTAACTTGGCCTGAAGCCTC-3', 'purchase_order': 201, 'date_received': datetime.date(2001, 1, 16), 'date_opened': datetime.date(2001, 1, 30), 'date_discarded': datetime.date(2001, 2, 27), 'stored_freezer': '-20/3', 'stored_box': 'H / 2'},
{'id': 304, 'primer_name': 'Ha-9-R', 'primer_seq': '5-TCTCCCTCCGAAGCAGGTTTCGCGG-3', 'purchase_order': 201, 'date_received': datetime.date(2001, 1, 16), 'date_opened': datetime.date(2001, 1, 30), 'date_discarded': datetime.date(2001, 2, 27), 'stored_freezer': '-20/3', 'stored_box': 'H / 3'},
{'id': 311, 'primer_name': 'Ha-10-F', 'primer_seq': '5-GTCCTGAATCATGTTTCCCCTGCAC-3', 'purchase_order': 205, 'date_received': datetime.date(2003, 12, 19), 'date_opened': datetime.date(2004, 1, 2), 'date_discarded': datetime.date(2004, 1, 30), 'stored_freezer': '-20/1', 'stored_box': 'F / 7'},] '...(remaining elements truncated)...']>
and that is correct, it is the search result that I want, so far so good. But then it just returns an empty template, it does not display any data.
This is my template:
<table id="search-results-primers-table">
<tr>
<th>Primer Name</th>
<th>Primer Sequence</th>
<th>Purchase Order</th>
<th>Date Received</th>
<th>Date Opened</th>
<th>Date Discarded</th>
<th>Freezer / Shelf</th>
<th>Box / Position</th>
</tr>
<tbody>
{% for item in result %}
<tr>
<td>{{ item.primer_name }}</td>
<td>{{ item.primer_seq }}</td>
<td>{{ item.purchase_order }}</td>
<td>{{ item.date_received }}</td>
<td>{{ item.date_opened }}</td>
<td>{{ item.date_discarded }}</td>
<td>{{ item.stored_freezer }}</td>
<td>{{ item.stored_box }}</td>
</tr>
{% endfor %}
</body>
</table>
I have tried iterating as explained here, I have reorganized result
using dictionary = {i: d for i, d in enumerate(queryset)}
, and whatever else I could find but it just won't work, I know the problem has to be in the result
that I am passing to render, or in {% for item in result %}
, but I am so stuck with this, I have spent hours on this and still no results. I need a pair of fresh eyes to take a look and tell me what is it I'm doing wrong.
CodePudding user response:
Since in the context, the queryset was passed in the keyword query
, you need to use that same keyword in the template so:
{% for item in query %}
...
About this error:
didn't return an HttpResponse object. It returned None instead.
This can happen in search_results_primers
if filter_by
is not 'primer_name', causing your view to return nothing. To fix it, make sure your view returns a response when filter_by
is not primer_name
.