搜索Django后重定向

时间:2017-05-06 11:23:40

标签: django django-request

您好我创建自动完成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 
                });       
            }
        });        
    });
  });

查看:

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

但它没有重定向game is not None它只是将页面响应到ajax请求。但我想在game != None时停止请求并加载我需要的页面。有什么问题?

0 个答案:

没有答案