2014-01-06 87 views
0

我不知道的jQuery或JavaScript真的,但我一直在關注這個教程:http://www.youtube.com/watch?v=jKSNciGr8kYDjango的:AJAX搜索停止工作

它一直致力於完美的罰款,直至在某些時候,它只是停止工作。現在,它的作用是,無論我到輸入字段中鍵入它返回數據庫中的所有信息。它應該返回「無顯示!」當SEARCH_TEXT不對應於數據庫中的信息,但它仍然會返回所有條目。

下面是它的視圖:

def search_infos(request): 
    if request.method == 'POST':  
     search_text = request.POST['search_text'] 
    else: 
     search_text = '' 
    u = request.user 
    custom_user = CustomUser.objects.get(user=u) 
    infos = Info.objects.filter(organization_name__contains = search_text, game__in=custom_user.game.all(), department__in = custom_user.department.all()).distinct() 
    return render_to_response('logins/ajax_search.html', {'infos' : infos}) 

下面是ajax_search模板:

{% if infos.count > 0 %} 

    {% for info in infos %} 
     <li><a href="{{ info.id }}/">{{ info.organization_name }}</a></li> 
    {% endfor %} 

{% else %} 

    <li>None to show!</li> 

{% endif %} 

這裏是AJAX文件本身:

$(function(){ 

    $('#search').keyup(function() { 

     $.ajax({ 
      type: "POST", 
      url: "/logins/search/", 
      data: { 
       'search_text' : $('#search').val(), 
       'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val() 
      }, 
      success: searchSuccess, 
      dataType: 'html' 
     }); 

    }); 

}); 

function searchSuccess(data, textStatus, jqXHR) 
{ 
    $('#search-results').html(data); 
} 

而且我也已經列入本

<script src="{% static "assets/js/jquery-2.0.0.min.js" %}"></script> 
    <script src="{% static "assets/js/ajax.js" %}"></script> 

在我base.html文件文件這樣的:

<div id = "search"> 
    <h3>Search</h3> 
    {% csrf_token %} 
    <input type="text" id="search" name="search" /> 
    <ul id="search-results"> 
    </ul> 
</div> 
中,我想有我在做什麼錯在

搜索框模板

提前致謝!

下面是型號:

from django.db import models 
from django.contrib.auth.models import User, UserManager 


class Department(models.Model): 
    name = models.CharField(max_length=200) 
    def __unicode__(self): 
     return self.name 

class Game(models.Model): 
    name_of_the_game = models.CharField(max_length=200) 
    def __unicode__(self): 
     return self.name_of_the_game 

class Info(models.Model): 
    organization_name = models.CharField(max_length=200) 
    url = models.URLField(max_length = 200, blank = True) 
    user_name = models.CharField(max_length=200, blank = True) 
    password = models.CharField(max_length=200, blank = True) 
    comments = models.TextField(max_length = 500, blank = True) 
    game = models.ManyToManyField(Game, blank = True) 
    department = models.ManyToManyField(Department, blank = True) 
    def __unicode__(self): 
     return self.organization_name+ ': '+ 'user name: ' +self.user_name+ ', '+ 'password: ' + self.password 


class CustomUser(models.Model): 
    department = models.ManyToManyField(Department) 
    game = models.ManyToManyField(Game) 
    user = models.OneToOneField(User) 

回答

2

問題是與你的HTML,你有兩個DOM元素命名爲 '搜索':

<div id = "search"> #here 
    <h3>Search</h3> 
    {% csrf_token %} 
    <input type="text" id="search" name="search" /> #and here 
    <ul id="search-results"> 
    </ul> 
</div> 

所以,當你做

$('#search').val() 

它獲取第一個對象,其ID爲「搜索」,這不是您的tex t輸入,但你的整個div,所以這個值總是一個空字符串。

在服務器端你有另一個問題,你允許空字符串獲取所有數據,使用@RaydelMiranda答案可以解決這個問題。

P.S

使用POST是指用於在數據庫中添加或更改數據。對於簡單的讀取,最好使用GET(然後你也不需要csrf)。我不確定本教程是在教你什麼,但也許你應該彼此學習django和ajax(我推薦nettut's jQuery in 30 days,完成了我的想法)

+0

+1爲好眼睛。 ;) –

2

我覺得你的問題是在視圖中。檢查這兩條線:

search_text = '' 

infos = Info.objects.filter(organization__name__contains = search_text, game__in=custom_user.game.all(), department__in = custom_user.department.all()).distinct() 

search_text是一個空字符串,也許

organization_name__contains = search_text 

得到總是true因爲'總是包含任何字符串內。

試試這個:

def search_infos(request): 
    infos = [] 
    if request.method == 'POST':  
     search_text = request.POST['search_text'] 
     u = request.user 
     custom_user = CustomUser.objects.get(user=u) 
     infos = Info.objects.filter(organization_name__contains = search_text, game__in=custom_user.game.all(), department__in = custom_user.department.all()).distinct() 
    return render_to_response('logins/ajax_search.html', {'infos' : infos}) 
+0

該死的,打我一秒鐘 – yuvi

+0

@RaydelMiranda謝謝爲您的迴應!我嘗試了你的建議,但它仍然不起作用:/ – lulu

+0

但是,你收到了一些錯誤消息,一些錯誤輸出? –