美文网首页
django搜索

django搜索

作者: 一念之间789 | 来源:发表于2018-09-30 15:16 被阅读0次

    book/url.py

    urlpatterns = [
        url(r'^hero_seach$', views.hero_seach, name="hero_seach"),
        ]
    

    book/hero_seach.html

    <script src="/static/book/js/jquery-1.12.4.min.js"></script>
    <script>
        $(function(){
            $("#hname").keyup(function(){
                $.ajax({
                    "url":"{% url "book:hero_seach" %}",
                    "data":{
                        "hname":$(this).val().trim()
                    },
                    "success":function(data){
                        $("#show ul").html("")
                        if(data.ret.length!=0){
                            data.ret.forEach(function (value) {
                            $("#show ul").append("<li>"+value["hname"]+"</li>")
                            })
                        }else{
                            $("#show ul").append("<li>无</li>")
                        }
                    }
                })
            })
        })
    </script>
    <body>
        <div>
            <input type="text" id="hname">
        </div>
        <div id="show">
            <ul></ul>
        </div>
    </body>
    

    book/views.py

    @check_user
    def hero_seach(request):
        hname = request.GET.get("hname")
        if hname:
            if hname=="":
                ret={"ret":[]}
                return HttpResponse(json.dumps(ret,ensure_ascii=False),content_type="application/json;charset=utf-8")
            hname_list = HeroInfo.objects.filter(hname__icontains=hname).values("hname","id")
            info = []
            for i in hname_list:
                d={}
                d["hname"]=i["hname"]
                d["hid"]=i["id"]
                info.append(d)
            ret={"ret":info}
            return HttpResponse(json.dumps(ret,ensure_ascii=False),content_type="application/json;charset=utf-8")
        return render(request, "book/hero_seach.html")

    相关文章

      网友评论

          本文标题:django搜索

          本文链接:https://www.haomeiwen.com/subject/ipswoftx.html