本文介绍了使用django以.csv格式搜索和导出数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用搜索和显示输出的搜索功能。包括搜索 fromdate和todate code>等。

In my application i am using a search function which search and display the output.I include search for fromdate and todate,Keyword search etc.

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

I want to export the searched result in a .csv file.Currently i had written a function called csv_export and exporting all the report in .csv.I want to know how to export the searched item in a .csv file.

forms.py搜寻

forms.py for search

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 for search

views.py for search

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

除了搜索按钮之外,还有一个保存电子表格是他们在同一个搜索页面上,点击搜索按钮搜索的项目显示,而点击保存电子表格按钮显示的项目导出为 .csv 档案。

Apart from search button a button called save-spreadsheet is their on the same search page,on clicking search button the searched items are come to display and while clicking save-spreadsheet button the displayed items are exported to .csv file.

需要协助。

感谢

推荐答案

检查

Check the django documentation about outputting CSV.

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

You can display a list of search results like:

<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:

And create a view that outputs a CSV for given report id:

urls.py

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

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

这篇关于使用django以.csv格式搜索和导出数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!