2018-10-08-Python全栈开发-day64-组合搜索

views

from django.shortcuts import render

# Create your views here.
from app01 import models

def index(request,*args,**kwargs):
    dict={}
    for k,v in kwargs.items():#此时用户输入的数据已经放入字典中,接下来获取用户输入的分类,难度的id
        temp=int(v)
        if temp != 0:
            dict[k]=temp
    if not dict:
        video_list = models.Video.objects.all()
    # classification_id=dict['classification_id']
    # level_id=dict['level_id']
    classification_list=models.Classification.objects.all()

    level_list=models.Level.objects.all()#得到分类和等级的所有值,传给前端

    video_list=models.Video.objects.filter(**dict)
    print(video_list)
    return render(request,'index.html',{
        'classification_list':classification_list,
        'level_list':level_list,
        'video_list':video_list,
        'kwargs':kwargs
    })

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .div1 a.active{
            background-color: coral;
            color: white;
        }
        .div1 a{
            display: inline-block;
            padding: 5px 8px;
            border: 1px solid #dddddd;
        }

    </style>
</head>
<body>
<div class="div1">
    <h1>筛选</h1>
        <div class="div1">
        {% if kwargs.classification_id == 0 %}

        {% endif %}
            <a href="/index-0-{{kwargs.level_id}}">全部</a>

            {% for item in classification_list %}
                {% if item.id == kwargs.classification_id %}
                    <a class="active" href="/index-{{ item.id }}-{{kwargs.level_id}}">{{ item.name }}</a>
                {% endif %}
                <a href="/index-{{ item.id }}-{{kwargs.level_id}}">{{ item.name }}</a>
            {% endfor %}
        </div>
        <div class="div2">
            <a href="/index-{{ kwargs.classification_id }}-0">全部</a>
            {% for item in level_list %}
                {% if item.id == kwargs.level_id %}
                    <a class="active" href="/index-{{ kwargs.classification_id }}-{{item.id}}">{{ item.title }}</a>
                {% endif %}
                <a href="/index-{{ kwargs.classification_id }}-{{item.id}}">{{ item.title }}</a>
            {% endfor %}
        </div>
</div>
<h1>结果</h1>
<div>
        {% for item in video_list %}
            {{ item.title }}
        {% endfor %}
    </div>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/hai125698/p/9757926.html