Note that there are some explanatory texts on larger screens.

plurals
  1. POsearch and export the data in .csv format using django
    primarykey
    data
    text
    <p>In my application i am using a search function which search and display the output.I include search for <code>fromdate and todate</code>,<code>Keyword search</code> etc.</p> <p>I want to export the searched result in a <code>.csv</code> file.Currently i had written a function called <code>csv_export</code> and exporting all the report in .csv.I want to know how to export the searched item in a .csv file.</p> <p>forms.py for search</p> <pre><code>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'})) </code></pre> <p>views.py for search</p> <pre><code>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,}) </code></pre> <p>Apart from search button a button called <code>save-spreadsheet</code> is their on the same search page,on clicking search button the searched items are come to display and while clicking <code>save-spreadsheet</code> button the displayed items are exported to <code>.csv</code> file.</p> <p>Need help to do this.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload