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

37 views
Skip to first unread message

Patrick Carra

unread,
Aug 28, 2020, 7:06:09 PM8/28/20
to Django users
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>

RANGA BHARATH JINKA

unread,
Aug 28, 2020, 10:05:04 PM8/28/20
to django...@googlegroups.com
Hi,
     
    Please check this package. This may help you. All the best

  

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

RANGA BHARATH JINKA

unread,
Aug 28, 2020, 10:26:21 PM8/28/20
to django...@googlegroups.com
Hi,

     Check this. 

RANGA BHARATH JINKA

unread,
Aug 28, 2020, 10:56:38 PM8/28/20
to django...@googlegroups.com
Hi,

    Use jQuery datatable for this. It is easy to use and has inbuilt search and export to CSV, Excel options also.

RANGA BHARATH JINKA

unread,
Aug 28, 2020, 11:01:36 PM8/28/20
to django...@googlegroups.com
Hi,


     There is a django module.
Message has been deleted

coolguy

unread,
Aug 28, 2020, 11:28:46 PM8/28/20
to Django users
You can't have the same name twice. You need to change the name for one of the followings:
    path('customsearch/', views.search, name='search'),
    path('customsearch/<str:params>/', views.search, name='search_xxx')

Reverse method requires a url identified by name parameter which must be unique. Its looking for search and it find it on two places so you are getting this error.

V. J

unread,
Aug 29, 2020, 2:37:24 PM8/29/20
to django...@googlegroups.com
Věřte si.... 😉 Vašek. J

Odesláno z mého telefonu Huawei


-------- Původní zpráva --------
Od: RANGA BHARATH JINKA <bharath...@gmail.com>
Datum: so 29. srp 2020 5:00
Komu: django...@googlegroups.com
Předmět: Re: NoReverseMatch at /searchlit/customsearch and bigger problem of exporting a filtered queryset

Patrick Carra

unread,
Aug 31, 2020, 1:47:16 PM8/31/20
to Django users
Thanks coolguy I changed my urls.py to this and that resolved the error:

from django.urls import include, path
from . import views
from .views import search

app_name= "searchLit"

urlpatterns=[
    path('customsearch/', views.search, name='search'),
    path('exportlit/', views.searchLit_export, name='SearchLit_export'),
    path('noccustomsearch/', views.nocSearch, name='nocSearch'),
    path('exportnoc/', views.noc_export, name='noc_export'),
    path('exportSearch/<str:params>/', views.exportSearch, name='exportSearch'),
]

I now am getting:

Reverse for 'exportSearch' with arguments '("{'circuitid': '', 'bandwidth': 'Select Bandwidth', 'region': 'Central', 'carrier': 'Select Carrier', 'status': 'Select Status', 'segmentname': 'Select Segment', 'mrcnew': '', 'diversity': 'Select Option', 'kmz': 'Select YES/NO', 'multipleSearch': ''}",)' not found. 1 pattern(s) tried: ['searchlit\\/exportSearch\\/(?P<params>[^/]+)\\/$']  

When I try to use this code in my template to pass a dictionary(containing my search parameters) to the export function to export only filtered table data:

template:
<a href="{% url 'searchLit:exportSearch' paramString %}"><input type="button" value="Self Destruct" /></a>


views.py:
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)

I assume that I need to use a different path converter but I am unsure.  Any tips am I thinking about this the right way or is there a better way to do what I am trying to do?


Patrick Carra

unread,
Aug 31, 2020, 2:03:49 PM8/31/20
to Django users
Thanks bharath for the info I'm going to dig more in to these.

RANGA BHARATH JINKA

unread,
Sep 1, 2020, 12:32:10 AM9/1/20
to django...@googlegroups.com
No problem.
All the best 😀



--
Thanks and Regards

J. Ranga Bharath
cell: 9110334114
Reply all
Reply to author
Forward
0 new messages