Hi,

    Please check this package. This may help you. All the best

    https://django-tables2.readthedocs.io/en/latest/pages/filtering.html


On Sat, 29 Aug 2020, 4:36 am Patrick Carra, <[email protected]> wrote:

> Hello All!  I need some help with a problem I have been struggling with.
> One of you generous geniuses has the answer I'm sure of it.  I am working
> on integrating an export to csv button(link) on my template to export
> filtered data from one of my tables.  I have previously only been able to
> dump the entire table to csv which isn't exactly what I need.  So my
> thoughts were to place a button/link on the template passes a
> parameter(dictionary of my filter criteria) to a view named ExportSearch.
> The view then calls a function that I have defined to perform my filtered
> query to the table and then writes outs to csv.  In theory I thought this
> should work but I keep getting errors that I have not been able to resolve
> and I am at the end of my knowledge capacity here.  Applicable code below.
> Did I mention THANK YOU!!!!
>
> *From searchLit/urls.py*
> from django.urls import include, path
> from . import views
> from .views import search
>
> app_name= "searchLit"
>
> urlpatterns=[
>     path('customsearch/', views.search, name='search'),
>     path('customsearch/<str:params>/', views.search, name='search'),
>     path('noccustomsearch/', views.nocSearch, name='nocSearch'),
>     path('nocreport/export/', views.noc_export, name='noc_export'),
>     path('customsearch/export/', views.searchLit_export,
> name='SearchLit_export'),
>     path('customsearch/exportSearch/<str:params>/', views.exportSearch,
> name='exportSearch'),
> ]
>
> *From searchLit/views.py*
> def filterCircuits(params, search_vector):
>     circuits=[]
>     if(params['circuitid']==None and params['bandwidth']==None and
> params['region']==None and params['carrier']==None and
> params['status']==None and params['segmentname']==None and
> params['mrcnew']==None):
>         circuits=Circuitinfotable.objects.all()
>     else:
>         if(params['multipleSearch']!=None and
> params['multipleSearch']!=""):
>
> circuits=Circuitinfotable.objects.annotate(search=search_vector).filter(search=params['multipleSearch'])
>         else:
>             circuits = Circuitinfotable.objects.all()
>         if(params['circuitid']!=None and params['circuitid']!=""):
>             circuits = Circuitinfotable.objects.all()
>
> circuits=circuits.filter(circuitid__icontains=params['circuitid'])
>         if(params['bandwidth']!=None and params['bandwidth']!="" and
> params['bandwidth']!='Select Bandwidth'):
>             circuits=circuits.filter(bandwidth=params['bandwidth'])
>         if(params['region']!=None and params['region']!="" and
> params['region']!='Select Region'):
>             circuits=circuits.filter(region=params['region'])
>         if(params['carrier']!=None and params['carrier']!="" and
> params['carrier']!='Select Carrier'):
>             circuits=circuits.filter(carrier=params['carrier'])
>         if(params['status']!=None and params['status']!="" and
> params['status']!='Select Status'):
>             circuits=circuits.filter(status=params['status'])
>         if(params['segmentname']!=None and params['segmentname']!="" and
> params['segmentname']!='Select Segment'):
>             circuits=circuits.filter(segmentname=params['segmentname'])
>         if(params['mrcnew']!=None and params['mrcnew']!=""):
>             circuits=circuits.filter(mrcnew=params['mrcnew'])
>         if(params['diversity']!='Select Option'):
>             if(params['diversity']=='None'):
>                 circuits=circuits.filter(diversity=None)
>             else:
>
> circuits=circuits.filter(diversity__icontains=params['diversity'])
>         if(params['kmz']!='Select YES/NO'):
>             if(params['kmz']=='No'):
>                 circuits=circuits.filter(kmz=None)
>             else:
>                 circuits=circuits.filter(kmz__isnull=False)
>     return(circuits)
>
>
> def search(request):
>     form = CircuitForm
>     template =  'customsearch/customsearch.html'
>     search_vector = SearchVector('circuitid', 'carrier', 'pathname',
> 'segmentname', 'segmentid', 'alocationaddress', 'alocationcity', 'alocst',
> 'zlocationaddress', 'zlocationcity', 'zlocst', 'handoffalocaddress',
> 'handoffalocst',
>                                  'handoffaloccity', 'handoffzlocaddress',
> 'handoffzloccity', 'handoffzlocst', 'latestjiraticket',
> 'installciopsticket', 'retermciopsticket', 'discociopsticket', 'notes',
> 'diversitynotes')
>
>     params={'circuitid': request.GET.get('circuitid'),
>             'bandwidth': request.GET.get('bandwidth'),
>             'region': request.GET.get('region'),
>             'carrier': request.GET.get('carrier'),
>             'status': request.GET.get('status'),
>             'segmentname': request.GET.get('segmentname'),
>             'mrcnew': request.GET.get('mrcnew'),
>             'diversity': request.GET.get('diversity'),
>             'kmz': request.GET.get('kmz'),
>             'multipleSearch': request.GET.get('multipleSearch')
>            }
>
>     context=filterCircuits(params, search_vector)
>     #encoded_params = urllib.parse.urlencode(params)
>     numCircuits=len(context)
>     paramString = str(params)
>     return render(request, template, {'form':form, 'filter':context,
> 'numcircuits':numCircuits, 'params':params, 'paramString': paramString})
>
>
> def exportSearch(request, params):
>     params = ast.literal_eval(params)
>     search_vector = SearchVector('circuitid', 'carrier', 'pathname',
> 'segmentname', 'segmentid', 'alocationaddress', 'alocationcity', 'alocst',
> 'zlocationaddress', 'zlocationcity', 'zlocst', 'handoffalocaddress',
> 'handoffalocst',
>                                  'handoffaloccity', 'handoffzlocaddress',
> 'handoffzloccity', 'handoffzlocst', 'latestjiraticket',
> 'installciopsticket', 'retermciopsticket', 'discociopsticket', 'notes',
> 'diversitynotes')
>     allcircuits=filterCircuits(params, search_vector)
>     return render_to_csv_response(allcircuits)
>
> *From template(button/link placed outside of form to capture filters
> called paramString)*
> <a href="{% url 'exportSearch' paramString %}"><input type="button"
> value="Self Destruct" /></a>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/bbf5ec72-955d-4d52-9c43-b00d0b17551dn%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/bbf5ec72-955d-4d52-9c43-b00d0b17551dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK5m317GUkhHLXJ7d0w4r29DFBa0ZtGH5BPwuutt1n5V8g%2BXJw%40mail.gmail.com.

Reply via email to