1

I'm using jQuery autocomplete plugin http://www.devbridge.com/projects/autocomplete/jquery/ to provide search suggestions in my web application where I want to send the response in json format.

Django views.py for sending the suggestions response:

def keywords_suggestions(request):
        if request.is_ajax():
                suggestions = []
                q = request.POST.get('q')
                try:
                        g = KeywordsModel.objects.filter(keyword__startswith=q).order_by('count')
                except KeywordsModel.DoesNotExist:
                        return HttpResponse("")
                else:
                        for i in range(0,len(g)):
                                global suggestions
                                suggestions.append(g[i].keyword)
                                to_json = {
                                        "query": q,
                                        "suggestions": suggestions
                                }
                        return HttpResponse(simplejson.dumps(to_json), mimetype='application/json')

Django models.py:

class KeywordsModel(models.Model):
        keyword = models.CharField(max_length=40, blank=False)
        count = models.IntegerField(max_length=20)

        def __unicode__(self):
            return self.keyword

jQuery code:

$("#add-keywords").keyup(function() {
    $('#add-keywords').autocomplete({ 
            serviceUrl:'/keywords_suggestions',
            minChars:3, 
            maxHeight:220,
            width:280,
            zIndex: 9999,
            params: { q: $('#add-keywords').val() },
            onSelect: function(value, data){ $('#add-keywords').val(value); },
    });
});

I'm getting this error when I type on the #add-keywords text box.

Request URL:http://127.0.0.1:8000/keywords_suggestions/?q=web&query=web
Request Method:GET
Status Code:500 INTERNAL SERVER ERROR

UPDATE

ValueError at /keywords_suggestions/
The view information.views.keywords_suggestions didn't return an HttpResponse object.

UPDATE-2

I'm having doubt in the suggestions variable, maybe global suggestions will have the problem. Am I doing it right?

Could anyone guide me to make it work?

UPDATE-3

<input type="text" id="add-keywords" name="add-keywords" title="e.g. Web developer, Javascript, Musician, Jazz" />

How to get the value of #add-keywords text box in the Django views.py. Does this work q = request.POST.get('add-keywords')?

Thanks!

rnk
  • 2,174
  • 4
  • 35
  • 57

2 Answers2

1

the judgement request.is_ajax() returns False

okm
  • 23,575
  • 5
  • 83
  • 90
iMom0
  • 12,493
  • 3
  • 49
  • 61
0

The condition branches

try:
    g = KeywordsModel.objects.filter(keyword__startswith=q).order_by('count')
except KeywordsModel.DoesNotExist:
    return HttpResponse("")
else:
    ...

also could fail as ValueError if, for example, request.POST.get('q') results None

Plus, try '/keywords_suggestions/', note the suffix slash, instead of '/keywords_suggestions' in the serviceUrl:'/keywords_suggestions', line

okm
  • 23,575
  • 5
  • 83
  • 90
  • Yes, the problem starts here `q = request.POST.get('q')` It is not actually getting the query value. Why is it so? I've done the jQuery code as shown in the documentation. – rnk Jun 08 '12 at 09:42