Django - Queryset结果不显示在模板中



我正在学习Django并为实验室构建一个库存应用程序。我在所有模型中都有数据,现在我想根据一些标准搜索数据库,并在表中显示结果。首先,我要求用户输入搜索条件(该部分有效),然后我查询数据库(这也有效),但当显示结果时,我得到的是一个空模板。无错误信息。

这些是我的观点:

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)

search_results_primers中的查询返回如下(在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)...']>

,这是正确的,这是我想要的搜索结果,到目前为止一切顺利。但是它只返回一个空模板,不显示任何数据。

这是我的模板:

<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>

我已经尝试迭代在这里解释,我已经用dictionary = {i: d for i, d in enumerate(queryset)}重组了result,以及其他任何我能找到的,但它只是不起作用,我知道问题必须在result,我正在传递渲染,或在{% for item in result %},但我是如此坚持这个,我已经花了几个小时,仍然没有结果。我需要一双新鲜的眼睛来看看,告诉我我做错了什么。

由于在上下文中,查询集是在关键字query中传递的,因此您需要在模板中使用相同的关键字,因此:

{% for item in query %}
...

关于这个错误:

没有返回HttpResponse对象。它返回None。

如果filter_by不是'primer_name',这可能发生在search_results_primers中,导致视图不返回任何内容。要解决这个问题,请确保当filter_by不是primer_name时,视图返回一个响应。

相关内容

  • 没有找到相关文章

最新更新