python - Django Search Bar Implementation -


i'm trying implement search bar query database , show matches. when hit submit gives me 'search', set default instead of printing error.

ajax.py

... def chunksearcher(request): test = request.get.get('search_box', "search") print(test) .... 

searcher.html

<form type="get" action="." style="margin: 0"> <input  id="search_box" type="text" name="search_box"  value="search..." > <button id="search_submit" type="submit" >submit</button> 

urls.py

 url(r'^ajax/chunk/searcher/$',     ajax.chunksearcher, name='chunksearcher') 

views.py (it works here reason won't recognize same 2 lines of code in ajax code

def searcher(request): # test = request.get.get('search_box', "search") # print(test) this_main = searcher(     request          = request,     num_elements     = candidate.objects.all().count(),     size             = 'col-xs-12',     title            = 'search',     modelname        = 'searcher',     listing_fields   = [         {'readable_name': 'name',   'model_attribute': 'full_name()', 'subtext_model': 'email', 'color': 'false'},         {'readable_name': 'status', 'model_attribute': 'get_status_display()', 'color': 'true'},         {'readable_name': 'automated status', 'model_attribute': 'get_auto_status()', 'color': 'true'},         {'readable_name': 'submitter', 'model_attribute': 'submitter', 'color': 'true'},     ],     listing_actions  = [         {'tooltip': 'search',  'color': 'success', 'icon': 'plus', 'permission': 'prog_port.add_candidate', 'modal': 'candidateform', 'controller': 'addcandidate'},     ], )  context = {     'nav'    : nav(request),     'main'   : this_main,     'fb'     : testfeedback() } return render(request, 'prog_port/base.html', context) 

widgets.py

class searcher: def __init__(self, request,                    num_elements,                    size                         = 'col-xs-12',                    modelname                    = none,                    title                        = none,                    listing_fields               = none,                    listing_actions              = none):#!!      self.template = 'prog_port/widgets/searcher.html'     self.size = size     self.modelname = modelname     self.num_elements = num_elements     self.num_pages = int(math.ceil( num_elements / 25.0))     self.title = title     self.listing_fields  = [x['readable_name'] x in listing_fields]     self.listing_actions = listing_actions      action in self.listing_actions:         action['restricted'] = false         if 'permission' in action:             if not request.user.has_perm(action['permission']):                 action['restricted'] = true 

getting working without ajax bit quicker start. when action attribute of form pointed towards url of current page (rather towards url of ajax view), request sent view corresponds page's url - searcher view in case. that's why able expected values print when had 2 lines in view.

importantly, since searcher view 1 rendering page, having access search_box value in view lets filter or otherwise manipulate queryset being passed view's context , display restricted/filtered items want shown.

a separate ajax view doesn't have access of stuff right off of bat. dynamically update search results separate ajax view, view need respond request of information necessary re-render page appropriately. practically speaking, means 1 of 2 things:

  1. your search results displayed within div or other defined content area, , ajax view returns html necessary populate content area appropriate stuff, or

  2. your initial view renders template based on serialized json, , ajax view provides updated information in format used re-render template.

this starting point getting hang of ajax django. notice in example code given how view responds ajax call data (a httpresponse or rendered template), , how data used in success/failure functions.

if ajax view returned html necessary render search results, use success function update search results div (or table or whatever) on page new html. example:

views.py

def index(request):     return render(request, "index.html")  def ajax_update(request):     return httpresponse("<h1>updated header</h1>") 

index.html

... <div id="update_this_header">     <h1>old header</h1> </div> <button id='updater'> ... <script> $("#updater").click(function() {     $.ajax({         url: #url ajax_update view         success : function(data) {             $("#update_this_header").html(data)         },         failure : function(data) {            ...         }     }); }); </script> 

now clicking updater button should update contents of update_this_header div html returned in httpresponse our ajax_update view (i admit didn't test this, forgive me if there's typo). updating search results works same way; need more processing in ajax view respond correct html.

i hope helps make things clearer; please let me know if can (try to) explain more fully. important takeaway here ajax view provide data. it's make sure template can take data , display it.


Comments