技术要点:
template
1、使用url携带参数,如 http://www.xxx.com/0-0-0-0-0.html
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>