目录
- BBS 项目(四)
- 首页布局
- 个人头像显示
- 个人站点路由设计
- 个人站点页面设计
- base.html
- site.html
- 左侧过滤功能
- 404.html
BBS 项目(四)
首页布局
Blog-index
{# 头部 #}
{# 主体 #}
{# 左侧 #}
Panel title
Panel content
Panel title
Panel content
Panel title
Panel content
{# 中间 #}
{# 文章 #}
{% for article in article_list %}
{% endfor %}
{# 右侧 #}
- Cras justo odio
- Dapibus ac facilisis in
- Morbi leo risus
- Porta ac consectetur ac
- Vestibulum at eros
{# 页脚 #}
个人头像显示
1 开启media
-配置文件
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
-路由
re_path('^media/(?P.*?)$', serve,kwargs={'document_root':settings.MEDIA_ROOT}),
-前端:
个人站点路由设计
1 路由一定要放在最后
re_path('^(?P\w+)$', views.personal_site),
个人站点页面设计
base.html
{% block title %}
{% endblock %}
{% block js %}
{% endblock %}
{% block site_name %}
{% endblock %}
{% block left %}
{% endblock %}
{% block main %}
{% endblock %}
site.html
{% extends 'base.html' %}
{% block title %}
{{ user.username }}-博客园
{% endblock %}
{% block site_name %}
{{ user.blog.site_name }}
{% endblock %}
{% block left %}
我的标签
{% for tag in tag_list %}
{% endfor %}
我的分类
{% for category in category_list %}
{% endfor %}
随笔档案
{% for month in month_list %}
{% endfor %}
{% endblock %}
{% block main %}
{% for article in article_list %}
{% endfor %}
{% endblock %}
左侧过滤功能
# 查询当前站点下某年某月的文章数(分组依据,日期:年月),不需要连表
from django.db.models.functions import TruncMonth, TruncDay, TruncYear, TruncHour
'''
Sales.objects
.annotate(month=TruncMonth('timestamp')) # Truncate to month and add to select list
.values('month') # Group By month
.annotate(c=Count('id')) # Select the count of the grouping
.values('month', 'c') # (might be redundant, haven't tested) select month and count
'''
month_list = models.Article.objects.filter(blog=user.blog).annotate(month = TruncMonth('create_time')).values('month').annotate(count = Count('id')).values_list('month','count')
id name create_time month
1 xx 2020-09-18:xxx 2020-09
2 xx44 2020-09-18:xxx 2020-09
3 xx44 2020-08-18:xxx 2020-08
'''
按月份阶段分组
'''
# 个人站点
def personal_site(request, username):
user = models.UserInfo.objects.filter(username=username).first()
if user:
article_list = user.blog.article_set.all()
# category_list = user.blog.category_set.all()
# tag_list = user.blog.tag_set.all()
# 查询每个分类下的文章数和分类名称
# category_list = models.Category.objects.values('id').annotate(count=Count('article')).values('count', 'name')
# 查询当前这个站点下的分类及分类下的文章数
category_list = models.Category.objects.filter(blog=user.blog).annotate(count=Count('article')).values('count',
'name')
# 查询当前站点下所有的标签及标签下的文章数
tag_list = models.Tag.objects.filter(blog=user.blog).annotate(count=Count('article')).values_list('count',
'name')
# 查询当前站点下按月分组的文章数和年月
from django.db.models.functions import TruncMonth # 按月截断
month_list = models.Article.objects.filter(blog=user.blog).annotate(month = TruncMonth('create_time')).values('month').annotate(count = Count('id')).values_list('month','count')
return render(request, 'site.html', locals())
else:
return render(request, '404.html')
404.html
Title