目录
一、分析博客园
"""
按照标签筛选
https://www.cnblogs.com/wupeiqi/tag/1/
按照分类筛选
https://www.cnblogs.com/wupeiqi/category/1/
按照时间筛选
https://www.cnblogs.com/wupeiqi/archive/2020-11/
"""
二、路由层的编写
# 侧边栏搜素功能
# ?wupeiqi/tag/1
# url(r'^(?P<username>\w+)/tag/(\d+)$', views.site),
# url(r'^(?P<username>\w+)/category/(\d+)$', views.site),
# url(r'^(?P<username>\w+)/archive/(\w+)$', views.site),
# 优化
re_path('^(?P<username>\w+)/(?P<condition>tag|category|archive)/(?P<param>.*)', views.site),
三、视图函数逻辑的实现
由于不知道具体传几个参数,所以用**kwargs接收
if kwargs:
# 代表筛选
# {'condition': 'tag', 'param': '1'}
# {'condition': 'category', 'param': '1'}
# {'condition': 'archive', 'param': '2020-11'}
print(kwargs)
condition = kwargs.get('condition')
param = kwargs.get('param')
if condition == 'category':
# 按照分类id进一步做筛选,筛选文章
article_list = article_list.filter(category__pk=param)
elif condition == 'tag':
# 跨表查询了 正向查询 查找的字段名小写
article_list = article_list.filter(tags__pk=param)
else:
year, month = param.split('-') # 2020-11 [2020, 11]
article_list = article_list.filter(create_time__year=year,
create_time__month=month)
四、给a超链接加地址
<a href="/{{ username }}/category/{{ cate.2 }}">{{ cate.0 }}({{ cate.1 }})</a>
<a href="/{{ username }}/tag/{{ tag.2 }}">{{ tag.0 }}({{ tag.1 }})</a>
<a href="/{{ username}}/archive/{{date.month|date:'Y-m' }}">{{ date.month|date:'Y-m' }}({{ date.c }})</a>
标签:category,功能,list,param,侧边,article,筛选,condition
From: https://www.cnblogs.com/yuezongke/p/17390679.html