我的Django应用程序中有一个“简单”搜索,它会生成一个数据库中所有命中的列表,与搜索词匹配。我希望用户能够单击一个按钮,该按钮将生成一个pdf报告,其中包含简单搜索中列出的命中的所有信息。因此,我想知道是否可以“存储”搜索生成的查询集并将其传递给链接到“生成PDF报告”按钮的函数。
当前的search.html模板:
{% extends 'base.html' %}
{% block body %}
{% if enzyme_list %}
<h1>Search Results (matching enzyme terms):</h1>
<ul>
{% for enzyme in enzyme_list %}
<li><a href="{% url 'gts:detail' enzyme.id %}">{{ enzyme.barcode }}, {{ enzyme.enzyme_name }}</a></li>
{% endfor %}
</ul>
<div>
<a href="{% url 'gts:pdf_report' %}" class="btn btn-primary">Generate PDF report (WORK IN PROGRESS)</a>
</div>
{% else %}
<h1>No enzyme(s) found matching the specified search term.</h1>
{% endif %}
{% endblock %}
views.py(显示两个相关视图)
# The view that is used to generate the initial simplified list (template shown above)
class EnzymeList(generic.ListView):
context_object_name = 'enzyme_list'
template_name = 'gts/search.html'
def get_queryset(self):
query = self.request.GET.get("q")
#queryset_list = Enzymes.objects.filter(barcode='Tt_86')
queryset_list = Enzymes.objects.filter(Q(barcode__icontains=query) |
Q(enzyme_name__icontains=query) | Q(ec_number__icontains=query) | Q(enzyme_activity__icontains=query) |
Q(organism__icontains=query) | Q(genbank_number__icontains=query) | Q(uniprot_accession__icontains=query) |
Q(description_of_activity__icontains=query) | Q(experiment_details__icontains=query)
)
return queryset_list
# The view that is linked to the PDF generation (just the 'framework' for now).
def ReportView(request):
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
# Create the PDF object, using the response object as its "file."
p = canvas.Canvas(response)
# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of functionality.
p.drawString(100, 100, "Hello world.")
# Close the PDF object cleanly, and we're done.
p.showPage()
p.save()
return response
与函数的URL匹配的urls.py
app_name = 'gts'
urlpatterns = [
url(r'^get_result/$', views.EnzymeList.as_view(), name='enzyme_list'),
url(r'^pdf_report/$', views.ReportView, name='pdf_report'),
]
我希望我能够在模板中执行类似<a href="{% url 'gts:pdf_report' enzyme_list %}" class="btn btn-primary">
和新功能头def ReportView(request, data):
的操作,但这会抱怨错过反向匹配。
- 潜在修复
我找到了一种让它工作的方法,但我确信这不正确'Django',我刚刚将问题添加到了返回中。因此,我可以将它发送回pdf代。 “糟糕”的解决方案如下:
views.py
class EnzymeList(generic.ListView):
context_object_name = 'enzyme_list'
template_name = 'gts/search.html'
def get_queryset(self):
query = self.request.GET.get("q")
#queryset_list = Enzymes.objects.filter(barcode='Tt_86')
queryset_list = Enzymes.objects.filter(Q(barcode__icontains=query) |
Q(enzyme_name__icontains=query) | Q(ec_number__icontains=query) | Q(enzyme_activity__icontains=query) |
Q(organism__icontains=query) | Q(genbank_number__icontains=query) | Q(uniprot_accession__icontains=query) |
Q(description_of_activity__icontains=query) | Q(experiment_details__icontains=query)
)
return (query, queryset_list)
search.html
{% extends 'base.html' %}
{% block body %}
{% if enzyme_list %}
<h1>Search Results (matching enzyme term: "{{ enzyme_list.0 }}"):</h1>
<ul>
{% for enzyme in enzyme_list.1 %}
<li><a href="{% url 'gts:detail' enzyme.id %}">{{ enzyme.barcode }}, {{ enzyme.enzyme_name }}</a></li>
{% endfor %}
</ul>
<div>
<a href="{% url 'gts:pdf_report' %}?q={{enzyme_list.0}}" class="btn btn-primary">Generate PDF report (WORK IN PROGRESS)</a>
</div>
{% else %}
<h1>No enzyme(s) found matching the specified search term: "{{ enzyme_list.0 }}".</h1>
{% endif %}
{% endblock %
}
- TL:DR -
然后问题具体是如何将搜索词传递给ReportView
函数,假设我的'fix'不是Django-esque。
答案 0 :(得分:1)
为什么不将查询移动到其他函数并在需要时随时调用它。
def enzyme_search(query):
return Enzymes.objects.filter(Q(barcode__icontains=query) |
Q(enzyme_name__icontains=query) | Q(ec_number__icontains=query) | Q(enzyme_activity__icontains=query) |
Q(organism__icontains=query) | Q(genbank_number__icontains=query) | Q(uniprot_accession__icontains=query) |
Q(description_of_activity__icontains=query) | Q(experiment_details__icontains=query)
)
然后在EnzymeList
班,
...
def get_queryset(self):
query = self.request.GET.get("q")
return enzyme_search(query)
def get_context_data(self):
context = super(EnzymeList, self).get_context_data(**kwargs)
context['query'] = self.request.GET.get("q")
return context
...
并在ReportView
...
query = self.request.GET.get("q")
enzyme_list = enzyme_search(query)
...
然后在你的锚标签中
<a href="{% url 'gts:pdf_report' %}?q={{ query }}" class="btn btn-primary">
这可以帮助您将查询转移到ReportView
。