我真的不知道jquery或javascript,但我一直在关注本教程:http://www.youtube.com/watch?v=jKSNciGr8kY
它一直工作得非常好,直到某个时候,它才停止工作。现在它做的是,无论我输入到输入字段,它都会返回数据库中的所有信息。它应该返回“无显示!”当search_text与数据库中的Information不对应时,它仍会返回所有条目。
以下是它的观点:
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)
答案 0 :(得分: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})
答案 1 :(得分:2)
问题在于你的html,你有两个名为'search'的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为“search”的第一个对象,这不是您的文本输入,而是整个div,因此该值始终为空字符串。
在服务器端你遇到了另一个问题,你允许空字符串获取所有数据,使用@RaydelMiranda回答这将解决它。
<强> P.S 强>
使用POST意味着用于添加或更改数据库中的数据。对于简单的提取,最好使用GET(然后你也不需要csrf)。我不确定这个教程是教你什么的,但也许你应该相互学习django和ajax(我推荐nettut's jQuery in 30 days,对我来说很奇怪)