Monday, August 31, 2020

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

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?


On Friday, August 28, 2020 at 10:28:46 PM UTC-5 coolguy wrote:
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.

On Friday, August 28, 2020 at 7:06:09 PM UTC-4 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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/99d1f423-4dcc-4dd2-87c1-a3a5240e120an%40googlegroups.com.

No comments:

Post a Comment