天天看点

django+Mysql+搜索

路由:
path('serchbook/<int:booksid>/', views.serch_book, name='serch_book'),

视图:
                def serch_book(request, booksid):
    if request.method == "GET":
        name = request.GET.get("booksname")
        country = request.GET.get("bookscountry")
        money = request.GET.get("booksmoney")
        if name != "" or money != "" or country != "":
            ob_list = Books.objects.all().order_by("-booksid")
        if name != "" or money != "" or country != "":
            ob_list = Books.objects.filter(booksname=name).filter(bookscountry=country).filter(
            booksmoney=money).order_by("-booksid")
        if name != "":
            ob_list = Books.objects.filter(booksname=name).order_by("-booksid")
        if money != "":
            ob_list = Books.objects.filter(booksmoney=money).order_by("-booksid")
        if country != "":
            ob_list = Books.objects.filter(bookscountry=country).order_by("-booksid")
        paginator = Paginator(ob_list, 10)
        page = request.GET.get("page", booksid)
        currentPage = int(page)
        try:
            video_list = paginator.page(page)
        except PageNotAnInteger:
            video_list = paginator.page(1)
        except EmptyPage:
            video_list = paginator.page(paginator.num_pages)
        return render(request, 'serchbooks.html', locals())      
静态页面:
                {% load static %}
<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>查看所有书信息</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" target="_blank" rel="external nofollow" 
          integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="{% static 'js/jquery-2.14.min.js' %}"></script>
    <script src="{% static 'js/alldelete.js' %}"></script>
</head>
<body>
<form action="{% url 'book:serch_book' 1 %}" method="get">{% csrf_token %}
    图书名称:<input type="text" name="booksname" placeholder="请输入图书名称">
    图书国籍:
    <select name="bookscountry">
        <option value="">请选择国家</option>
        <option value="中国">中国</option>
        <option value="美国">美国</option>
        <option value="英国">英国</option>
    </select>
    图书价格:<input type="text" name="booksmoney" placeholder="请输入价格">
    <button>搜索</button>
</form>
<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="{% url 'book:add_book' %}" target="_blank" rel="external nofollow" ><span
                class="label label-success">添加信息</span></a></li>
    </ol>
</nav>
<form action="{% url 'book:del_allbook' %}" method="post">{% csrf_token %}
    <div class="alert alert-warning" role="alert">
        Allbooks/查看所有书
    </div>
    <button type="button" class="btn btn-outline-info" οnclick="ALL_check()">全选</button>
    <button type="button" class="btn btn-outline-info" οnclick="ALL_uncheck()">反选</button>
    <button type="submit" class="btn btn-outline-warning">删除</button>
    <br>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>全选/反选</th>
            <th>ID编号</th>
            <th>图书种类</th>
            <th>图书名称</th>
            <th>入库几册</th>
            <th>图书国籍</th>
            <th>是否入库</th>
            <th>图书价格</th>
            <th>添加时间</th>
            <th>封面文件路径</th>
            <th>图书封面</th>
            <th>管理信息</th>
        </tr>
        </thead>
        <tbody>
        {% for i in video_list %}
            <tr>
                <td id="testTbody"><input type='checkbox' name="booksid" value="{{ i.booksid }}"/></td>
                <td>{{ i.booksid }}</td>
                <td>{{ i.booksspecies }}</td>
                <td>{{ i.booksname }}</td>
                <td>{{ i.booksnumber }}</td>
                <td>{{ i.bookscountry }}</td>
                {% if i.booksstorage == True %}
                    <td>是</td>
                {% else %}
                    <td>否</td>
                {% endif %}
                <td>¥{{ i.booksmoney }}</td>
                <td>{{ i.bookstimes }}</td>
                <td>{{ i.booksimg_name }}</td>
                {% if i.booksimg == "" %}
                    <td style="color: crimson;">没有图片</td>
                {% else %}
                    <td>
                        {% if i.booksimg|length > 35 %}
                            {{ i.booksimg|safe|slice:"15:35" }}...
                        {% else %}
                            {{ i.booksimg }}
                        {% endif %}
                    </td>
                {% endif %}
                <td><a href="{% url 'book:del_book' i.booksid %}" target="_blank" rel="external nofollow" >删除</a> |
                    <a href="{% url 'book:update_book' i.booksid %}" target="_blank" rel="external nofollow" >修改</a>
                </td>
            </tr>
            {% empty %}
            <tr>
                <td colspan="12" align="center">
                    您没有搜索到相关信息
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    <nav aria-label="Page navigation example">
        <ul class="pagination">
            <li class="page-item">
                {% if video_list.has_previous %}
                    <a class="page-link" href="{% url 'book:serch_book' video_list.previous_page_number %}" target="_blank" rel="external nofollow" 
                       aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                {% else %}
                    <a class="page-link" href="#" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                {% endif %}
            </li>
            {% for num in paginator.page_range %}
                {% if num == currentPage %}
                    <li class="page-item"><a class="page-link"
                                             href="{% url 'book:serch_book' num %}" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >{{ num }}</a></li>
                {% else %}
                    <li class="page-item"><a class="page-link"
                                             href="{% url 'book:serch_book' num %}" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >{{ num }}</a></li>
                {% endif %}
            {% endfor %}
            <li class="page-item">
                {% if video_list.has_next %}
                    <a class="page-link" href="{% url 'book:serch_book' video_list.next_page_number %}" target="_blank" rel="external nofollow" 
                       aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                {% else %}
                    <a class="page-link" href="#" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                {% endif %}
            </li>
        </ul>
    </nav>
</form>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"
        integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
        integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
        integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
        crossorigin="anonymous"></script>
</body>
</html>