简体   繁体   English

Django jquery-ui数据库自动完成

[英]Django jquery-ui Autocomplete with database

I want to use jquery-ui autocomplete plugin with my model. 我想在模型中使用jquery-ui自动完成插件。 I have this model: 我有这个模型:

class Baslik(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    title = models.CharField(max_length=50)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)

To make this done I used the codes below: input: 为此,我使用了以下代码:输入:

<input id="n" type="text" name="n"/>

js: js:

<script>
$(document).ready(function(){
     $( "input#n" ).autocomplete({
                            source: "{% url "autoco" %}",
                            minLength: 2
        });
});
</script>

view: 视图:

def autoco(request):
     term = request.GET.get('term')
     bslk = Baslik.objects.filter(title__istartswith=term)
     res = []
     for b in bslk:
          dict = {'id':b.id, 'label':b.__unicode__(), 'value':b.__unicode__()}
          res.append(dict)
     return HttpResponse(simplejson.dumps(res))

url: 网址:

url(r'^autoco/$', 'autoco', name='autoco'),

but it still does not work. 但它仍然不起作用。 When I just use local values in js code it works fine but in this case I cannot get any auto complete. 当我仅在js代码中使用局部值时,它可以正常工作,但在这种情况下,我无法获得任何自动完成功能。 When I type something to input, terminal shows a log like "GET /autoco/?term=se HTTP/1.1" 500 9892 What I did wrong. 当我输入一些内容时,终端会显示类似"GET /autoco/?term=se HTTP/1.1" 500 9892的日志"GET /autoco/?term=se HTTP/1.1" 500 9892我做错了什么。 Any opinion would help. 任何意见都会有所帮助。 Thanks. 谢谢。

The problem is in your urls.py. 问题出在您的urls.py中。 You can't refer to the view just as the string 'autoco' . 您不能像字符串'autoco'一样引用视图。 Either refer to it as 'myapp.views.autoco' , or import the actual view function and refer to it directly as autoco without the quotes. 可以将其称为'myapp.views.autoco' ,或者导入实际的视图函数,然后直接将其称为autoco而不使用引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM