将 Django 中的搜索结果下载到 csv
download to csv the result of a search in Django
我正在尝试从搜索结果中下载 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
我四处寻找不同的解决方案,但 none 实际上调试了我。知道怎么做吗?
我错过了什么?不能把两者结合起来吗?
def binder_search(request):
if request.method != "POST":
return render(request, "binder_search.html", {})
# POST
searched = request.POST['searched']
binders_searched = Binders.objects.filter(Q(description__contains=searched) | Q(step__name__contains=searched) | Q(status__name__contains=searched))
# create a csv writer with header writing to response
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=binders_result.csv'
writer = csv.writer(response)
writer.writerow(['Item Code', 'Description', 'Item Type', 'Current Step', 'Current Status', 'Last change by'])
# loop thru filtered results and output
for binder in binders_searched:
writer.writerow(
[binder.itemcode, binder.description, binder.itemtype, binder.step, binder.status, binder.user]
)
return response
这是一个有效的解决方案——在 nigel222 和会话框架 (request.session) 的帮助下完成。该解决方案提供了将搜索结果显示到模板中所需的视图,然后按按钮或 link 将该确切结果下载为 CSV 格式。因此,用户可以获得搜索结果,然后根据需要下载该结果。我一直在四处寻找,但找不到类似的解决方案,所以我将其张贴在这里供其他人使用。
第一个视图 (binder_search) 进行搜索并使用 request.session 缓存变量“searched”。
def binder_search(request):
if request.method == "GET":
searched = request.GET['searched']
# Cache variable "searched" for views.binder_search_csv
request.session['searched'] = searched
# multiple columns
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", {})
第二个视图使用上面视图中的缓存变量“searched”并将搜索结果下载到 csv 文件。
def binder_search_csv(request):
if request.method != "GET":
return render(request, "binder_search.html", {})
# Use cached variable from views.binder_search
searched = request.session['searched']
binders_searched = Binders.objects.filter(Q(description__contains=searched) | Q(step__name__contains=searched) | Q(status__name__contains=searched))
# create a csv writer with header writing to response
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=binders_result.csv'
writer = csv.writer(response)
writer.writerow(['Item Code', 'Description', 'Item Type', 'Current Step', 'Current Status', 'Last change by'])
# loop thru filtered results and output
for binder in binders_searched:
writer.writerow(
[binder.itemcode, binder.description, binder.itemtype, binder.step, binder.status, binder.user]
)
return response
可能有一些方法可以简化这两个视图,但就目前而言,它们工作得很好。
我正在尝试从搜索结果中下载 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
我四处寻找不同的解决方案,但 none 实际上调试了我。知道怎么做吗?
我错过了什么?不能把两者结合起来吗?
def binder_search(request):
if request.method != "POST":
return render(request, "binder_search.html", {})
# POST
searched = request.POST['searched']
binders_searched = Binders.objects.filter(Q(description__contains=searched) | Q(step__name__contains=searched) | Q(status__name__contains=searched))
# create a csv writer with header writing to response
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=binders_result.csv'
writer = csv.writer(response)
writer.writerow(['Item Code', 'Description', 'Item Type', 'Current Step', 'Current Status', 'Last change by'])
# loop thru filtered results and output
for binder in binders_searched:
writer.writerow(
[binder.itemcode, binder.description, binder.itemtype, binder.step, binder.status, binder.user]
)
return response
这是一个有效的解决方案——在 nigel222 和会话框架 (request.session) 的帮助下完成。该解决方案提供了将搜索结果显示到模板中所需的视图,然后按按钮或 link 将该确切结果下载为 CSV 格式。因此,用户可以获得搜索结果,然后根据需要下载该结果。我一直在四处寻找,但找不到类似的解决方案,所以我将其张贴在这里供其他人使用。
第一个视图 (binder_search) 进行搜索并使用 request.session 缓存变量“searched”。
def binder_search(request):
if request.method == "GET":
searched = request.GET['searched']
# Cache variable "searched" for views.binder_search_csv
request.session['searched'] = searched
# multiple columns
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", {})
第二个视图使用上面视图中的缓存变量“searched”并将搜索结果下载到 csv 文件。
def binder_search_csv(request):
if request.method != "GET":
return render(request, "binder_search.html", {})
# Use cached variable from views.binder_search
searched = request.session['searched']
binders_searched = Binders.objects.filter(Q(description__contains=searched) | Q(step__name__contains=searched) | Q(status__name__contains=searched))
# create a csv writer with header writing to response
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=binders_result.csv'
writer = csv.writer(response)
writer.writerow(['Item Code', 'Description', 'Item Type', 'Current Step', 'Current Status', 'Last change by'])
# loop thru filtered results and output
for binder in binders_searched:
writer.writerow(
[binder.itemcode, binder.description, binder.itemtype, binder.step, binder.status, binder.user]
)
return response
可能有一些方法可以简化这两个视图,但就目前而言,它们工作得很好。