Django jquery-ui数据库自动完成 [英] Django jquery-ui Autocomplete with database

查看:123
本文介绍了Django jquery-ui数据库自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在模型中使用jquery-ui自动完成插件. 我有这个模型:

I want to use jquery-ui autocomplete plugin with my model. 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:

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

视图:

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'),

,但仍然无法正常工作.当我只在js代码中使用局部值时,它可以正常工作,但在这种情况下,我无法获得任何自动完成功能.当我键入要输入的内容时,终端会显示类似"GET /autoco/?term=se HTTP/1.1" 500 9892的日志,我做错了什么.任何意见都会有所帮助.谢谢.

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. When I type something to input, terminal shows a log like "GET /autoco/?term=se HTTP/1.1" 500 9892 What I did wrong. Any opinion would help. Thanks.

推荐答案

问题出在您的urls.py中.您不能像字符串'autoco'那样引用视图.要么将其称为'myapp.views.autoco',要么导入实际的视图函数,然后直接将其称为autoco而不带引号.

The problem is in your urls.py. You can't refer to the view just as the string '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.

这篇关于Django jquery-ui数据库自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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