From 476c674bc24de822e13357fe3a650476b0edb296 Mon Sep 17 00:00:00 2001 From: Seth Woodworth Date: Tue, 29 Jan 2013 19:39:51 -0500 Subject: [PATCH] fixing school autocomplete for add course box --- karmaworld/apps/courses/views.py | 15 ++++---- karmaworld/templates/lightbox/add_course.html | 35 +++++++++++++++---- karmaworld/urls.py | 2 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/karmaworld/apps/courses/views.py b/karmaworld/apps/courses/views.py index 5172bb5..d35ede7 100644 --- a/karmaworld/apps/courses/views.py +++ b/karmaworld/apps/courses/views.py @@ -4,6 +4,7 @@ import json +from django.http import HttpResponse from django.views.generic import DetailView from django.views.generic import TemplateView @@ -31,12 +32,14 @@ class AboutView(TemplateView): def school_list(request): """ Return JSON describing Schools that match q query on name """ - if request.method == 'GET' and request.is_ajax() \ - and request.GET.has_key('q'): + if request.method == 'POST' and request.is_ajax() \ + and request.POST.has_key('q'): # if an ajax get request with a 'q' name query - # get the list of schools that match and return - schools = School.objects.filter(name__icontains=request.GET['q'])[:4] - return json.dump({'status':'success', 'schools': schools}) + # get the schools as a id name dict, + schools = [{'id': s.id, 'name': s.name} for s in \ + School.objects.filter(name__icontains=request.POST['q'])[:4]] + # return as json + return HttpResponse(json.dumps({'status':'success', 'schools': schools}), mimetype="application/json") else: # else return that the api call failed - return json.dump({'status':'fail'}) + return HttpResponse(json.dump({'status':'fail'}), mimetype="application/json") diff --git a/karmaworld/templates/lightbox/add_course.html b/karmaworld/templates/lightbox/add_course.html index dcfae1c..8a5a712 100644 --- a/karmaworld/templates/lightbox/add_course.html +++ b/karmaworld/templates/lightbox/add_course.html @@ -1,16 +1,32 @@ {% load url from future %} @@ -46,7 +68,8 @@ School diff --git a/karmaworld/urls.py b/karmaworld/urls.py index 49a2861..c100163 100644 --- a/karmaworld/urls.py +++ b/karmaworld/urls.py @@ -45,7 +45,7 @@ urlpatterns = patterns('', # uploading files url(r'^ajax-upload$', ajax_uploader, name='ajax_upload'), # return json list of schools - url(r'^j/school/list$', school_list, name='json_school_list'), + url(r'^school/list/$', school_list, name='json_school_list'), # ---- end JSON views ----# url(r'^$', ListView.as_view(model=Course), name='home'), -- 2.25.1