Django 学习 Blog 实操 11 --- 侧边栏点击显示相应内容

一、任务

  • 侧边栏的归档,分类,标签云点击显示相应内容

二、步骤

  • views.py 中写查看分类的方法
  • blog/urls.py 中配置访问路径
  • 在对应的分类标签中填写正确的访问路径

三、代码

blog/views.py

def archive(request, year, month):
    post_list = Post.objects.filter(created_time__year=year,
                                    created_time__month=month).order_by('-created_time')
    return render(request, 'blog/index.html', context={'post_list': post_list})


def category(request, pk):
    cate = get_object_or_404(Category, pk=pk)
    post_list = Post.objects.filter(category=cate).order_by("-created_time")
    return render(request, 'blog/index.html', context={'post_list': post_list})


def tag(request, pk):
    tag = get_object_or_404(Tag, pk=pk)
    post_list = Post.objects.filter(tags=tag).order_by("-created_time")
    return render(request, 'blog/index.html', context={'post_list': post_list})

blog/urls.py

path('blog/archives/<int:year>/<int:month>/', views.archive, name='archive'),
path('blog/categories/<int:pk>/', views.category, name='category'),
path('blog/tags/<int:pk>/', views.tag, name='tag'),

templates/blog/inclusions/_archives.html, _categories.html, _tags.html

 <a href="{% url 'blog:archive' date.year date.month %}">{{ date.year }} {{ date.month }}</a>
<a href="{% url 'blog:category' category.id %}">{{ category.name }} <span class="post-count">(13)</span></a>
 <a href="{% url 'blog:tag' tag.pk %}">{{ tag.name }}</a>

四、问题

  • django 中 path的末尾的/加不加什么区别?
  • blog 的 url 配置前面加不加blog呢?

附上 Gitee 地址:https://gitee.com/langxing/HelloDjango-blog-tutorial

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sapphire~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值