下载到django搜索的结果CSV

发布于 2025-01-24 17:55:10 字数 1384 浏览 4 评论 0原文

我正在尝试从搜索的结果下载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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2025-01-31 17:55:10

我想念什么?你不能只结合两个吗?

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

What am I missing? Can't you just combine the two?

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
暮凉 2025-01-31 17:55:10

这是一个解决方案,可以在Nigel222和会话框架(请求。Session)的帮助下进行。该解决方案提供了将搜索结果显示到模板中所需的视图,然后按按钮或链接将确切的结果下载到CSV格式中。因此,用户可以获取搜索结果,然后如果他愿意下载结果。我一直在环顾四周,但找不到类似的解决方案,所以我在这里将其发布在这里。

第一个视图(binder_search)进行搜索和使用请求,以缓存变量“搜索”。

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", {})

第二视图使用从上面的视图“搜索”的缓存变量,然后将搜索结果下载到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

可能有一些方法可以简化这两种观点,但是正如现在一样,它们的工作完全很好。

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

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", {})

The second view use the cached variable "searched" from the above view and download the search result to a csv file.

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

There might be ways to streamline these two views but as they are right now, they work totally fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文