Djnago组合查询和瀑布流

前端之家收集整理的这篇文章主要介绍了Djnago组合查询和瀑布流前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一对多组合查询/搜索


技术要点:


        template


        1、使用url携带参数,如 http://www.xxx.com/0-0-0-0-0.html


        2、通过伪造静态页面路径,增加搜索引擎收录


        views


        1. 通过路由分发获取指定参数


                url(r'^video-(?P<classification_id>(\d+))-(?P<level_id>(\d+)).html$',views.video),


        2. 使用**进行多个条件查询


代码示例:


------------------views.py------------------

from django.shortcuts import render

from app01 import models

def video(request,*args,**kwargs):

    condition = {

        # 'level_id': 0,

        # 'classification_id': 0,

    }

    for k,v in kwargs.items():

        temp = int(v)

        kwargs[k] = temp

        if temp:

            condition[k] = temp

    class_list = models.Classification.objects.all()

    level_list = models.Level.objects.all()

    video_list = models.Video.objects.filter(**condition)

    return render(

        request,

        'video.html',

        {

            'class_list':class_list,

            'level_list':level_list,

            'kwargs':kwargs,

            'video_list':video_list,

        }

    )

video.html


<!DOCTYPE html>

<html>

<head>

    <Meta charset="UTF-8">

    <title>Title</title>

    <style>

        .condition a{

            display: inline-block;

            padding: 5px 8px;

            border: 1px solid #dddddd;

        }

        .condition a.active{

            background-color: coral;

            color: white;

        }

    </style>

</head>

<body>

    <div>

        <h1>筛选</h1>

        <div>

            {% if kwargs.classification_id == 0 %}

                <a href="/video-0-{{ kwargs.level_id }}.html">全部</a>

            {% else %}

                 <a  href="/video-0-{{ kwargs.level_id }}.html">全部</a>

            {% endif %}

            {% for item in class_list %}

                {% if item.id == kwargs.classification_id %}

                    <a href="/video-{{ item.id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a>

                {% else %}

                    <a href="/video-{{ item.id }}-{{ kwargs.level_id }}.html">{{ item.name }}</a>

                {% endif %}

            {% endfor %}

        </div>

        <div>

            <a href="/video-{{ kwargs.classification_id }}-0.html">全部</a>

            {% for item in level_list %}

                {% if item.id == kwargs.level_id %}

                    <a href="/video-{{ kwargs.classification_id }}-{{ item.id }}.html">{{ item.title }}</a>

                {% else %}

                    <a href="/video-{{ kwargs.classification_id }}-{{ item.id }}.html">{{ item.title }}</a>

                {% endif %}

            {% endfor %}

        </div>

    </div>

    <div>

        <h1>结果</h1>

        <div>

            {% for row in video_list %}

                <div>{{ row.title }}</div>

            {% endfor %}

        </div>

    </div>

</body>

</html>


猜你在找的Django相关文章