我有搜索功能,我还提供用户将搜索结果导出为csv。我的搜索功能正常。但是,导出功能无法收集用户完成的搜索结果。我能理解这个问题背后的原因,但无法找到解决问题的方法。 (使用Global变量可能会有所帮助,但我不想使用它) 我的搜索查询功能:
def search_query(request):
result = publication.objects.all()
query = request.GET.get("query")
if query:
result = result.filter(
Q(title__icontains=query)
Q(timeStamp__icontains=query)
).distinct()
user_filter = UserFilter(request.GET, queryset=result)
return user_filter
我的搜索功能:
def search(request):
title = "Advanced Search"
user_filter = search_query(request)
context = {
'title': title,
'filter': user_filter,
}
return render(request, "search.html", context)
我的导出功能:
def export_search_csv(request):
data = search_query(request)
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;filename=publications.csv'
writer = csv.writer(response)
writer.writerow(['Title', 'Published On'])
for item in data.qs:
writer.writerow([item.title, item.timeStamp.strftime("%b-%Y")])
return response
UPD:
html(导出部分):
<p>
<h5>
<a href="{% url 'publication:export_search_csv' %}?{{ request.GET.urlencode }}" class="btn btn-default" style="width: 150px; role="button">
<span class="glyphicon glyphicon-save"> </span> Export to CSV</a>
</h5>
</p>
main urls.py:
urlpatterns = [
url(r'^', include('publications.urls', namespace='publication')),
]
出版物/ urls.py:
urlpatterns = [
url(r'^export/csv/$', views.export_search_csv, name='export_search_csv'),
]
请帮忙。
追踪(最近一次通话):
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/response.py", line 107, in render
self.content = self.rendered_content
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/response.py", line 84, in rendered_content
content = template.render(context, self._request)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 207, in render
return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/template/defaulttags.py", line 458, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/urls/base.py", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/Django-1.11-py2.7.egg/django/urls/resolvers.py", line 497, in _reverse_with_prefix
raise NoReverseMatch(msg)
NoReverseMatch: Reverse for 'export_search_csv' not found. 'export_search_csv' is not a valid view function or pattern name.
答案 0 :(得分:0)
如果您在模板中使用导出到csv链接,可以尝试使用此链接指定当前获取参数,如下所示:
<a href="{% url 'export_search_csv' %}?{{ request.GET.urlencode }}">export to csv</a>
这将允许将当前过滤器参数传递给导出视图。