我有一个输入名称的表单。它搜索数据库中的lastname和firstname字段。当用户输入名称时,我需要自动完成。我被建议在这个链接后使用jQuery django jquery
但它没有用。我在评论中尝试了所有的建议,而不是作品。我的模特
class Pitable(models.Model):
pid = models.TextField(db_column='PID', primary_key=True) # Field name made lowercase.
lname = models.TextField(blank=True, null=True)
fname = models.TextField(blank=True, null=True)
locs = models.TextField(blank=True, null=True)
doclist = models.TextField(db_column='PMIDlist', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'PItable'
我的观点:
import json
def get_people(request):
if request.is_ajax():
q = request.GET.get('term', '')
persons = Pitable.objects.filter(lname__icontains = q )[:20]
results = []
for person in persons:
person_json = {}
person_json['id'] = person.pid
person_json['label'] = person.lname
person_json['value'] = person.lname
results.append(person_json)
data = json.dumps(results)
#data = json.dumps(list(Pitable.objects.filter(lname__icontains=q).values('lname')))
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data, mimetype)
网址:
url(r'^api/get_people/', views.get_people, name='get_people'),
HTML:
<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.18/themes/base/jquery-ui.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
</script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
<script>
$(function() {
$("#names").autocomplete({
source: "/api/get_people/",
minLength: 2,
});
});
</script>
.......
<div class="ui-widget">
<label for="names">Drugs: </label>
<input id="names">
</div>
runserver端说
Not Found: /api/get_people/
[21/Oct/2016 16:36:05] "GET /api/get_people/?term=ra HTTP/1.1" 404 2065
感谢您的帮助