1

在我的应用程序中,我使用了一个搜索功能来搜索和显示输出。我包括搜索fromdate and todateKeyword search

我想将搜索结果导出到一个文件中。.csv目前我已经编写了一个名为csv_export并将所有报告导出到 .csv 中的函数。我想知道如何将搜索到的项目导出到 .csv 文件中。

用于搜索的 forms.py

class SearchFilterForm(Form):
    location = forms.ChoiceField(widget=forms.Select(), choices='',required=False)
    type = forms.ChoiceField(widget=forms.Select(), choices='',required=False)
    fromdate = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'dd/mm/yyyy','class':'datefield','readonly':'readonly'}))
    todate = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'dd/mm/yyyy','class':'datefield','readonly':'readonly'})) 
    search_keyword = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Keyword Search','class':'keyword-search'}))

用于搜索的 views.py

def search(request):
    """Search reports using filters
    """
    user = request.user
    report_list = []
    searchfilter = SearchFilterForm(user) 
    reports = Report.objects.filter(user=user)
    if request.method == 'POST':
       if 'search' in request.POST:
            search_keyword = request.POST.get('search_keyword') #reports filter by keywords
            reports = reports.filter(Q(incident_description__icontains=search_keyword)|Q(incident_number__icontains=search_keyword))
        elif 'filter' in request.POST:
            searchfilter = SearchFilterForm(user,request.POST)
            loc_id = request.POST.get('location')
            type_id = request.POST.get('type')
            start_date = request.POST.get('fromdate')
            end_date = request.POST.get('todate')
            reportlist = []
            """"""""""" #some stuff for search come here
            if start_date or end_date:                 
                if start_date and not end_date:
                    reports = reports.filter(created_date_time__gte=start_date)
                elif not start_date and end_date:
                    reports = reports.filter(created_date_time__lte=end_date)
                elif start_date and end_date:
                    reports = reports.filter(created_date_time__gt=start_date,created_date_time__lt=end_date)
    for report in reports:
      """"""  report iteration goes here
        report_list.append(items)
    return render(request, 'incident/search.html',
                   {'SearchKeywordForm':searchform,})

除了搜索按钮之外,save-spreadsheet在同一搜索页面上还有一个称为按钮的按钮,单击搜索按钮时,搜索到的项目会显示出来,单击save-spreadsheet按钮时,显示的项目会导出到.csv文件中。

需要帮助才能做到这一点。

谢谢

4

1 回答 1

0

检查有关输出 CSV 的 django 文档

您可以显示搜索结果列表,例如:

<ul>
    <li><form method="GET" action="/reports/1/csv/"><input type="submit" value="Get Report 1 csv" /></form></li>
    <li><form method="GET" action="/reports/30/csv/"><input type="submit" value="Get Report 30 csv" /></from></li>
    ...
</ul>

并为给定的报告 ID 创建一个输出 CSV 的视图:

网址.py

url(r'^reports/(?P<report_id>\d+)/csv/$', 'reports.views.csv_report')

视图.py

def csv_report(request, report_id):
    report = Reports.objects.get(pk=report_id)
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="report.csv"'
    response.write(report.to_csv())  # TODO to_csv
    return response
于 2013-06-28T09:11:18.400 回答