1

Hi i create autocomplete seacrh

Javascript

jQuery(function() {
    $("#id_input").on('keyup', function(){
        var value = $(this).val();
        $.ajax({
            url: "{% url 'ajax_autocomplete' %}",
            data: {
              'search': value 
            },
            dataType: 'json',
            success: function (data) {
                list = data.list;
                $("#id_input").autocomplete({
                source: list,
                minLength: 3 
                });       
            }
        });        
    });
  });

View:

def autocomplete_search(request):
      if request.is_ajax():
            game = None
            try:
                game = Game.objects.get(name=request.GET.get('search', None))
            except:
                pass
            if game is not None:
                result = redirect('games')
            else:    
                queryset = Game.objects.filter(name__startswith=request.GET.get('search', None))
                list = []
                for i in queryset:
                    list.append(i.name)
                data = {
                    'list': list,
                }
                result = JsonResponse(data)
            return result

But it didn't redirect when the game is not None it just response the page to ajax request. But i want to stop the request when the game != None and load the page i need. What is wrong?

Hellbea
  • 289
  • 6
  • 14
  • Take a look at this post: http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – karthikr May 06 '17 at 12:32

2 Answers2

0

Its because you are assigning the redirect function into a variable, it won't work that way. You just need to return at each end of condition.

TRy something like this:

def autocomplete_search(request):
      if request.is_ajax():
            game = None
            try:
                game = Game.objects.get(name=request.GET.get('search', None))
            except:
                pass
            if game is not None:
                return redirect('games')
            else:    
                queryset = Game.objects.filter(name__startswith=request.GET.get('search', None))
                list = []
                for i in queryset:
                    list.append(i.name)
                data = {
                    'list': list,
                }
                return JsonResponse(data)
zaidfazil
  • 9,017
  • 2
  • 24
  • 47
0

thx for karthikr i did it next way

def autocomplete_search(request):
      if request.is_ajax():
            game = None
            try:
                game = Game.objects.get(name=request.GET.get('search', None))
            except:
                pass
            if game is not None:
                data = {
                    'redirect': '/link-to-redirect/'
                }
                result = redirect('games')
            else:    
                queryset = Game.objects.filter(name__startswith=request.GET.get('search', None))
                list = []
                for i in queryset:
                    list.append(i.name)
                data = {
                    'list': list,
                }
                result = JsonResponse(data)
            return result

And jquery change next way:

        $.get({% url 'ajax_autocomplete' %}, {
                data: $('#search-game').val()
            }, function (response) {
                if (response.redirect !== undefined) {
                    top.location.href=response.redirect;
                } else {
                list = response.list;
                $("#search-game").autocomplete({
                    source: list,
                    minLength: 2
                });
                }
            });
Hellbea
  • 289
  • 6
  • 14