django和jQuery自动完成无效 [英] django and jQuery autocomplete not working

查看:244
本文介绍了django和jQuery自动完成无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其输入作为名称。它搜索数据库中的lastname和firstname字段。当用户键入名称时,我需要自动完成。我建议在这个链接后使用jQuery
django jquery



但它不工作。我在评论中尝试了所有的建议,nothings的作品。我的模型

  class Pitable(models.Model):
pid = models.TextField(db_column ='PID' primary_key = True)#字段名称小写。
lname = models.TextField(blank = True,null = True)
fname = models.TextField(blank = True,null = True)
locs = models.TextField(blank = null = True)
doclist = models.TextField(db_column ='PMIDlist',blank = True,null = True)#字段名称小写。

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 =个人的

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。过滤器(lname__icontains = q).values('lname'))
else:
data ='fail'
mimetype ='application / json'
return HttpResponse(data, mimetype)

urls:

  url(r'^ api / get_people /',views.get_people,name = 'get_people'),

html:

 < link rel =stylesheethref =http://code.jquery.com/ui/1.8.18/themes/base/jquery-ui.csstype = text / cssmedia =all/> 
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.jstype =text / javascript>
< / script> < script src =https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.jstype =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方面说

 未找到:/ api / get_people / 
[21 / Oct / 2016 16:36:05]GET / api / get_people /?term = ra HTTP / 1.1404 2065

感谢您的帮助

解决方案

我试过这个:我的网址:

  from django.conf.urls import url 
from 。导入视图
app_name ='chinook'
urlpatterns = [
url(r'piquery / $',views.QueryView.as_view(),name ='piquery'),
url(r'^ api / get_people / $',views.get_people,name ='get_people'),
]

在function()中,我更改了

  source:/ api / get_people /,

  source:api / get_people /,

现在,runserver输出是

 找不到:/ chinook / piquery / api / get_people / 
[21 / Oct / 2016 17:55:33]GET / chinook / piquery / api / get_people /?term = ra HTTP / 1.1404 3677

piquery is输入的html页面。我需要使runserver GET / chinook / api / get_people / ...,但如何做?谢谢!


I have a form which has input as name. It search the lastname and firstname field in a database. I need to autocomplete when users type the name. I was suggested to use jQuery following this link django jquery

But it's not working. I tried all the suggestion in the comments, nothings works. My models

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'

My views:

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)

urls:

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>

The runserver side says

Not Found: /api/get_people/
[21/Oct/2016 16:36:05] "GET /api/get_people/?term=ra HTTP/1.1" 404 2065

Thanks for your help

解决方案

I tried this: my urls:

from django.conf.urls import url
from . import views
app_name = 'chinook'
urlpatterns = [
    url(r'piquery/$', views.QueryView.as_view(), name='piquery'),
    url(r'^api/get_people/$', views.get_people, name='get_people'),
]

In function(), I changed

source: "/api/get_people/",

to

source: "api/get_people/",

Now, the runserver output is

Not Found: /chinook/piquery/api/get_people/
[21/Oct/2016 17:55:33] "GET /chinook/piquery/api/get_people/?term=ra HTTP/1.1" 404 3677

piquery is the html page where the input is. Do I need to make the runserver GET /chinook/api/get_people/..., but how to do it? Thanks!

这篇关于django和jQuery自动完成无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆