下载到django搜索的结果CSV
我正在尝试从搜索的结果下载CSV,作为一个选项。这意味着用户应该能够进行搜索并在模板中查看结果,然后根据需要下载该搜索结果的CSV。模板不是问题,而是我需要解决的观点。我有以下视图:
首先是搜索视图,
def binder_search(request):
if request.method == "POST":
searched = request.POST['searched']
binders_searched = Binders.objects.filter(Q(description__contains=searched) | Q(step__name__contains=searched) | Q(status__name__contains=searched))
return render(request, "binder_search.html", {'searched': searched, 'binders_searched': binders_searched})
else:
return render(request, "binder_search.html", {})
然后是CSV。此视图创建数据库中所有项目的列表。我要做的是从上面的视图中获得搜索结果,然后然后创建CSV文件。我最终会得到一个只有搜索结果的CSV文件。
def binders_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=binders_result.csv'
# create a csv writer
writer = csv.writer(response)
# designate the model
binders = Binders.objects.all()
# add column at the heading of csv file
writer.writerow(['Item Code', 'Description', 'Item Type', 'Current Step', 'Current Status', 'Last change by'])
# loop thru and output
for binder in binders:
writer.writerow([binder.itemcode, binder.description, binder.itemtype, binder.step, binder.status, binder.user])
return response
我环顾四周不同的解决方案,但实际上没有一个调试我。知道该怎么做吗?
I am trying to download a CSV from the result of a search, as an option. Meaning that the user should be able to do a search and view the result in a template, and then download the csv for that search result as needed. The templates are not the issue, it's the views I need to resolve. I have the following views:
First is the search view
def binder_search(request):
if request.method == "POST":
searched = request.POST['searched']
binders_searched = Binders.objects.filter(Q(description__contains=searched) | Q(step__name__contains=searched) | Q(status__name__contains=searched))
return render(request, "binder_search.html", {'searched': searched, 'binders_searched': binders_searched})
else:
return render(request, "binder_search.html", {})
Then is the csv. This view creates the list of all items in the database. What I am trying to do is get the search result from the above view, and then create the csv file. I would end up with a CSV file that has only the search result in it.
def binders_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=binders_result.csv'
# create a csv writer
writer = csv.writer(response)
# designate the model
binders = Binders.objects.all()
# add column at the heading of csv file
writer.writerow(['Item Code', 'Description', 'Item Type', 'Current Step', 'Current Status', 'Last change by'])
# loop thru and output
for binder in binders:
writer.writerow([binder.itemcode, binder.description, binder.itemtype, binder.step, binder.status, binder.user])
return response
I looked around at different solutions, but none actually debugged me. Any idea how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想念什么?你不能只结合两个吗?
What am I missing? Can't you just combine the two?
这是一个解决方案,可以在Nigel222和会话框架(请求。Session)的帮助下进行。该解决方案提供了将搜索结果显示到模板中所需的视图,然后按按钮或链接将确切的结果下载到CSV格式中。因此,用户可以获取搜索结果,然后如果他愿意下载结果。我一直在环顾四周,但找不到类似的解决方案,所以我在这里将其发布在这里。
第一个视图(binder_search)进行搜索和使用请求,以缓存变量“搜索”。
第二视图使用从上面的视图“搜索”的缓存变量,然后将搜索结果下载到CSV文件。
可能有一些方法可以简化这两种观点,但是正如现在一样,它们的工作完全很好。
Here is a solution that works--done with the help of nigel222 and the session framework (request.session). This solution provides the views needed for displaying a search result into a template, and then press a button or link to download that exact result into a CSV format. Therefore, the user can get a search result, and then download the result if he wants to. I have been looking around but couldn't find a similar solution, so I am posting it here for others.
The first view (binder_search) does the search and use request.session to cache the variable "searched".
The second view use the cached variable "searched" from the above view and download the search result to a csv file.
There might be ways to streamline these two views but as they are right now, they work totally fine.