daily pastebin goal
48%
SHARE
TWEET

Untitled

a guest Sep 23rd, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. *views.py*
  2. def get_members(request):
  3. if 'query' in request.GET:
  4.     q = request.GET.get('query', '').capitalize()
  5.     member_list = []
  6.     member_dict = {}
  7.     members = Member.objects.all().filter(
  8.         Q(last_name__icontains=q) |
  9.         Q(first_name__icontains=q) |
  10.         Q(number__icontains=q)
  11.         ).distinct()
  12.     for m in members:
  13.         member_list.append(m.search_result)
  14.     member_dict['options'] = member_list
  15.     data = json.dumps(member_dict)
  16.     mimetype = 'application/json'
  17.     return HttpResponse(data, mimetype)
  18. return HttpResponse()
  19.    
  20. *models.py*
  21. @property
  22. def search_result(self):
  23.     "Returns the member's number: full name in autocomplete search."
  24.     return '%s: ' '%s %s' % (self.number,self.first_name,self.last_name)
  25.  
  26. *html template*
  27. {% csrf_token %}
  28. <input type="search" name="searched_item" id="searched_item" class="form-
  29. control" autocomplete="off" placeholder="Enter member name to be searched
  30. (eg. John Smith)" style="max-width: 700px;width: 700px;color:
  31. threeddarkshadow;">
  32.     <script>
  33.     $(document).ready(function($) {
  34.       $('#searched_item').typeahead({
  35.         items:12,
  36.         source: function (query, process) {
  37.           return $.get('/membership/search/', { query: query }, function (data) {
  38.             return process(data.options);
  39.           });
  40.         },
  41.         minLength:1,
  42.         autoSelect:false,
  43.  
  44.         highlighter: function (item) {
  45.           var regex = new RegExp( '(' + this.query + ')', 'gi' );
  46.           return item.replace( regex, "<strong style='color:#7dcc79;' >$1</strong>" );
  47.         },
  48.       });
  49.     })
  50.   </script>
  51.  
  52. *urls.py*
  53. path('search/',views.search_members,name='autocomplete member names')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top