Monday, August 31, 2020

Re: NoReverseMatch at /searchlit/customsearch and bigger problem of exporting a filtered queryset

Thanks bharath for the info I'm going to dig more in to these.

On Friday, August 28, 2020 at 9:05:04 PM UTC-5 bharath...@gmail.com wrote:
Hi,
     
    Please check this package. This may help you. All the best

  

On Sat, 29 Aug 2020, 4:36 am Patrick Carra, <pcar...@gmail.com> 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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bbf5ec72-955d-4d52-9c43-b00d0b17551dn%40googlegroups.com.

--
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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d9121985-c53c-4b10-8c68-d85e71fe95e0n%40googlegroups.com.

No comments:

Post a Comment