在Django中试用jQuery的Autocomplete插件,实现自动提示功能

Friday, January 29, 2010

jQuery Autocomplete Plugin Home
Page

<!--autocomplete_demo.html-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>autocomplete</title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/demo/main.css" type="text/css" />
        <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type="text/css" />
        <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js"></script>
        <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.dimensions.js"></script>
        <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"></script>
        <script>
            $(document).ready(function () {
                $("#tag").autocomplete(
                    '/autocomplete/',
                    {multiple: true, multipleSeparator: ' '}
                );
            });
        </script>
    </head>
<body>
    Tag:<input id="tag"/>
</body>
</html>


#models.py
class Tag(models.Model):
    name = models.CharField('tag',max_length=100)


#views.py
def tag_autocomplete(request):
    if request.GET.has_key('q'):
        print request.GET['q']
        tags = Tag.objects.filter(name__icontains=request.GET['q'])[:10]
        return HttpResponse('\n'.join(tag.name for tag in tags))
    return HttpResponse()


#urls.py
(r'^autocomplete/$', tag_autocomplete),

This entry was tagged Django, jQuery and js

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top