我不知道的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)
+1爲好眼睛。 ;) –