嗨,我創建的自動完成seacrh重定向後,搜索的Django
的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
和加載我需要的網頁。哪裏不對?
看看這個post:http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery -ajax-call – karthikr