如何使用一些数据将数据发送到Django中的“ views.py”

时间:2020-07-03 19:18:37

标签: javascript python django

这是我发送数据的HTML

<div class="advanced-search">
                        <!-- <button type="button" class="category-btn">All Categories</button> -->
                        <select class='category-btn col-12' id='Catagory_search'>
                            <option value="Shop">IN SHOP</option>
                            <option value="Blog">IN BLOG</option>
                            <option value="Faq">IN FAQ</option>
                        </select>
                        <form action="" class="input-group" style="float: right;" id='AdvancseSearch'>
                            <input type="text" name='search' placeholder="What do you need?">
                            <button type="submit"><i class="ti-search"></i></button>
                        </form>
                    </div>

这里是我用来发送带有数据(搜索)的GET请求的Ajax

 $('#AdvancseSearch').on('submit', function(e) {
        ctg = document.getElementById('Catagory_search').value

        $.ajax({
            type: 'GET',
            async: true,
            url: "/Search/serach/" + ctg,
            data: {
                'search': $('input[name=search]').val()
            },
            encode: true,
        })
    })

将重定向的views.py函数

def Search(request,ctg):
search = request.GET.get('search')
if ctg == "Shop":
    d = Item.objects.filter(item_titile__icontains=search)
    send_dict = {'items':d}
    return render(request,'home/product.html',send_dict)

问题在于,当我将数据发送到views.py时,它成功发送了数据“搜索” 但它不会渲染我所希望的(home / product.html)页面,而是停留在当前页面上

0 个答案:

没有答案