BBS 项目(四)
作者:互联网
BBS 项目(四)
首页布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog-index</title>
<script src="/static/element/jQuery3.4.js"></script>
<script src="/static/element/bootstrap.min.js"></script>
<link rel="stylesheet" href="/static/element/bootstrap.min.css">
<link rel="stylesheet" href="/static/element/bootstrap.min.css.map">
<link rel="stylesheet" href="/static/element/font-awesome-4.7.0/css/font-awesome.css">
<style>
footer#footer {
padding-top: 32px;
padding-bottom: 32px;
display: flex;
flex-direction: column;
align-items: center;
color: #888;
background-color: #f3f3f3;
font-size: 13px;
font-weight: 400;
text-align: center;
}
footer {
display: block;
}
body {
font-family: "PingFang SC", "Microsoft YaHei", "Helvetica Neue", "Helvetica", "Arial", sans-serif;
font-weight: normal;
background-color: #fcf9f9;
}
.article_footer{
margin-right: 10px;
}
#underline{
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container-fluid">
{# 头部 #}
<div class="head">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<li class="navbar-branding">
<a href="/index/" class="navbar-brand" title="开发者的网上家园" role="banner">
博客园
</a>
</li>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="/index/">首页 <span class="sr-only">(current)</span></a></li>
<li><a href="https://news.cnblogs.com/">新闻</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">发现 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="https://zzk.cnblogs.com/">找找看</a></li>
<li><a href="#">收藏</a></li>
<li><a href="https://www.lagou.com/">招聘</a></li>
<li role="separator" class="divider"></li>
<li><a href="/login/">个人园子</a></li>
</ul>
</li>
</ul>
{% if request.user.is_authenticated %}
<ul class="nav navbar-nav navbar-right">
<li><a href="#">{{ request.user.username }}</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">更多操作 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/">个人主页</a></li>
<li><a href="#">设置</a></li>
<li><a href="admin">后台管理</a></li>
<li role="separator" class="divider"></li>
<li><a href="/logout/">退出</a></li>
</ul>
</li>
</ul>
{% else %}
<ul class="nav navbar-nav navbar-right">
<li><a href="/login/">登录</a></li>
<li><a href="/register/">注册</a></li>
</ul>
{% endif %}
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
{# 主体 #}
<div class="body row">
{# 左侧 #}
<div class="col-md-2">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
Panel content
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
Panel content
</div>
</div>
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
Panel content
</div>
</div>
</div>
{# 中间 #}
<div class="col-md-7">
<div class="lunbotu">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
{% for banner in banner_list %}
{% if forloop.first %}
<div class="item active">
<img src="{{ banner.url }}" alt="首页图">
{# <div class="carousel-caption">#}
{# {{ banner.name }}#}
{# </div>#}
</div>
{% else %}
<div class="item ">
<img src="{{ banner.url }}" alt="首页图">
</div>
{% endif %}
{% endfor %}
</div>
{# 轮播图按钮 #}
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span aria-hidden="true"><i class="fa fa-angle-double-left fa-3x" style="margin-top: 180px"></i></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span aria-hidden="true"><i class="fa fa-angle-double-right fa-3x" style="margin-top: 180px"></i></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
{# 文章 #}
<div class="article" style="margin-top: 30px">
{% for article in article_list %}
<div>
<h4 class="media-heading"><a href="" id="underline">{{ article.title }}</a></h4>
<div class="media">
<div class="media-left media-middle">
<a href="#">
<img class="media-object" src="/media/{{ article.blog.userinfo.avatar }}" alt="用户主页头像" height="50px" width="50px">
{# <img class="media-object" src="/static/img/96327268.jpg" alt="用户主页头像" height="50px" width="50px">#}
</a>
</div>
<div class="media-body">
{{ article.desc }}
</div>
<div style="margin-top: 10px">
<span class="article_footer"><a href="" class="table-hover" >{{ article.blog.userinfo.username }}</a></span>
<span class="article_footer">{{ article.create_time|date:'Y-m-d H:i:s' }}</span>
{# <span class="glyphicon glyphicon-thumbs-up article_footer"> {{ article.up_num }}</span>#}
<span class="article_footer"><a href=""><i class="fa fa-thumbs-up fa-lg"></i></a> {{ article.up_num }}</span>
<span class="article_footer"><a href=""><i class="fa fa-thumbs-down fa-lg"></i></a> {{ article.down_num }}</span>
<span class='article_footer'><a href=""><i class="fa fa-comment fa-lg"></i></a> {{ article.commit_num }}</span>
</div>
</div>
<hr>
</div>
{% endfor %}
</div>
</div>
{# 右侧 #}
<div class="col-md-3">
<ul class="list-group">
<li class="list-group-item">Cras justo odio</li>
<li class="list-group-item">Dapibus ac facilisis in</li>
<li class="list-group-item">Morbi leo risus</li>
<li class="list-group-item">Porta ac consectetur ac</li>
<li class="list-group-item">Vestibulum at eros</li>
</ul>
</div>
</div>
{# 页脚 #}
<div>
<br><br><br><br><br>
</div>
<div class="footer text-center" style="background-color: rgb(243,243,243)">
<footer id="footer" class="footer">
<div>
<!--done-->
Copyright © 2022 HammerZe
<br><span id="poweredby">Powered by .NET 6 on Kubernetes</span>
<span class="esa-copyright">& Theme <a href="https://github.com/esofar/cnblogs-theme-silence"
target="_blank">Silence v3.0.0</a></span></div>
</footer>
</div>
</div>
</body>
</html>
个人头像显示
1 开启media
-配置文件
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
-路由
re_path('^media/(?P<path>.*?)$', serve,kwargs={'document_root':settings.MEDIA_ROOT}),
-前端:
<img class="media-object" src="media/{{ article.blog.userinfo.avatar }}" alt="..." height="60px" width="60px">
个人站点路由设计
1 路由一定要放在最后
re_path('^(?P<username>\w+)$', views.personal_site),
个人站点页面设计
base.html#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
{% block title %}
{% endblock %}
</title>
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css">
{% block js %}
{% endblock %}
</head>
<body>
<div class="head" style="margin-bottom: 10px">
<div style="height: 120px;background-color: #1b6d85">
<div class="row">
<div class="col-md-2" style="font-size: 30px;color: white;margin: 20px">
{% block site_name %}
{% endblock %}
</div>
</div>
<div class="row">
<div class="pull-right" style="margin-right: 20px">
<span><a href="">后台管理</a></span>
<span>首页</span>
<span>订阅</span>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
{% block left %}
{% endblock %}
{% block main %}
{% endblock %}
</div>
</div>
</body>
</html>
site.html#
{% extends 'base.html' %}
{% block title %}
{{ user.username }}-博客园
{% endblock %}
{% block site_name %}
{{ user.blog.site_name }}
{% endblock %}
{% block left %}
<div class="col-md-2">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">我的标签</h3>
</div>
<div class="panel-body">
{% for tag in tag_list %}
<p><a href="">{{ tag.1 }}({{ tag.0 }})</a></p>
{% endfor %}
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">我的分类</h3>
</div>
<div class="panel-body">
{% for category in category_list %}
<p><a href="">{{ category.name }}({{ category.count }})</a></p>
{% endfor %}
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">随笔档案</h3>
</div>
<div class="panel-body">
{% for month in month_list %}
<p><a href="">{{ month.0|date:'Y年m月' }}({{ month.1 }})</a></p>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
{% block main %}
<div class="col-md-10">
<div class="article" style="margin-top: 20px">
{% for article in article_list %}
<div>
<h4 class="media-heading"><a href="">{{ article.title }}</a></h4>
<div class="media">
<div class="media-body">
{{ article.desc }}
</div>
<div style="margin-top: 10px" class="pull-right">
<span class="article_footer">posted @</span>
<span class="article_footer">{{ article.create_time|date:'Y-m-d H:i:s' }}</span>
{# <span class="glyphicon glyphicon-thumbs-up article_footer"> {{ article.up_num }}</span>#}
<span class="article_footer"><i
class="fa fa-comment-o fa-lg"></i> {{ article.up_num }}</span>
<span class="article_footer"><i
class="fa fa-home fa-lg"></i> {{ article.down_num }}</span>
<span class='article_footer'><i
class="fa fa-frown-o fa-lg"></i> {{ article.commit_num }}</span>
</div>
</div>
<hr>
</div>
{% endfor %}
</div>
</div>
{% 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-1 text-center" style="margin-top: 50px;margin-left: 100px">
<img src="/static/img/404.webp" alt="404图片">
</div>
<div class="col-md-10 col-md-offset-1 text-center" style='margin-top: 20px'>
<button class="btn btn-info btn-block"><a href="/index/">返回首页</a></button>
</div>
</div>
</div>
</body>
</html>
BBS 项目(四)
首页布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog-index</title>
<script src="/static/element/jQuery3.4.js"></script>
<script src="/static/element/bootstrap.min.js"></script>
<link rel="stylesheet" href="/static/element/bootstrap.min.css">
<link rel="stylesheet" href="/static/element/bootstrap.min.css.map">
<link rel="stylesheet" href="/static/element/font-awesome-4.7.0/css/font-awesome.css">
<style>
footer#footer {
padding-top: 32px;
padding-bottom: 32px;
display: flex;
flex-direction: column;
align-items: center;
color: #888;
background-color: #f3f3f3;
font-size: 13px;
font-weight: 400;
text-align: center;
}
footer {
display: block;
}
body {
font-family: "PingFang SC", "Microsoft YaHei", "Helvetica Neue", "Helvetica", "Arial", sans-serif;
font-weight: normal;
background-color: #fcf9f9;
}
.article_footer{
margin-right: 10px;
}
#underline{
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container-fluid">
{# 头部 #}
<div class="head">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<li class="navbar-branding">
<a href="/index/" class="navbar-brand" title="开发者的网上家园" role="banner">
博客园
</a>
</li>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="/index/">首页 <span class="sr-only">(current)</span></a></li>
<li><a href="https://news.cnblogs.com/">新闻</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">发现 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="https://zzk.cnblogs.com/">找找看</a></li>
<li><a href="#">收藏</a></li>
<li><a href="https://www.lagou.com/">招聘</a></li>
<li role="separator" class="divider"></li>
<li><a href="/login/">个人园子</a></li>
</ul>
</li>
</ul>
{% if request.user.is_authenticated %}
<ul class="nav navbar-nav navbar-right">
<li><a href="#">{{ request.user.username }}</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">更多操作 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/">个人主页</a></li>
<li><a href="#">设置</a></li>
<li><a href="admin">后台管理</a></li>
<li role="separator" class="divider"></li>
<li><a href="/logout/">退出</a></li>
</ul>
</li>
</ul>
{% else %}
<ul class="nav navbar-nav navbar-right">
<li><a href="/login/">登录</a></li>
<li><a href="/register/">注册</a></li>
</ul>
{% endif %}
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div>
{# 主体 #}
<div class="body row">
{# 左侧 #}
<div class="col-md-2">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
Panel content
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
Panel content
</div>
</div>
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
Panel content
</div>
</div>
</div>
{# 中间 #}
<div class="col-md-7">
<div class="lunbotu">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
{% for banner in banner_list %}
{% if forloop.first %}
<div class="item active">
<img src="{{ banner.url }}" alt="首页图">
{# <div class="carousel-caption">#}
{# {{ banner.name }}#}
{# </div>#}
</div>
{% else %}
<div class="item ">
<img src="{{ banner.url }}" alt="首页图">
</div>
{% endif %}
{% endfor %}
</div>
{# 轮播图按钮 #}
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span aria-hidden="true"><i class="fa fa-angle-double-left fa-3x" style="margin-top: 180px"></i></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span aria-hidden="true"><i class="fa fa-angle-double-right fa-3x" style="margin-top: 180px"></i></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
{# 文章 #}
<div class="article" style="margin-top: 30px">
{% for article in article_list %}
<div>
<h4 class="media-heading"><a href="" id="underline">{{ article.title }}</a></h4>
<div class="media">
<div class="media-left media-middle">
<a href="#">
<img class="media-object" src="/media/{{ article.blog.userinfo.avatar }}" alt="用户主页头像" height="50px" width="50px">
{# <img class="media-object" src="/static/img/96327268.jpg" alt="用户主页头像" height="50px" width="50px">#}
</a>
</div>
<div class="media-body">
{{ article.desc }}
</div>
<div style="margin-top: 10px">
<span class="article_footer"><a href="" class="table-hover" >{{ article.blog.userinfo.username }}</a></span>
<span class="article_footer">{{ article.create_time|date:'Y-m-d H:i:s' }}</span>
{# <span class="glyphicon glyphicon-thumbs-up article_footer"> {{ article.up_num }}</span>#}
<span class="article_footer"><a href=""><i class="fa fa-thumbs-up fa-lg"></i></a> {{ article.up_num }}</span>
<span class="article_footer"><a href=""><i class="fa fa-thumbs-down fa-lg"></i></a> {{ article.down_num }}</span>
<span class='article_footer'><a href=""><i class="fa fa-comment fa-lg"></i></a> {{ article.commit_num }}</span>
</div>
</div>
<hr>
</div>
{% endfor %}
</div>
</div>
{# 右侧 #}
<div class="col-md-3">
<ul class="list-group">
<li class="list-group-item">Cras justo odio</li>
<li class="list-group-item">Dapibus ac facilisis in</li>
<li class="list-group-item">Morbi leo risus</li>
<li class="list-group-item">Porta ac consectetur ac</li>
<li class="list-group-item">Vestibulum at eros</li>
</ul>
</div>
</div>
{# 页脚 #}
<div>
<br><br><br><br><br>
</div>
<div class="footer text-center" style="background-color: rgb(243,243,243)">
<footer id="footer" class="footer">
<div>
<!--done-->
Copyright © 2022 HammerZe
<br><span id="poweredby">Powered by .NET 6 on Kubernetes</span>
<span class="esa-copyright">& Theme <a href="https://github.com/esofar/cnblogs-theme-silence"
target="_blank">Silence v3.0.0</a></span></div>
</footer>
</div>
</div>
</body>
</html>
个人头像显示
1 开启media
-配置文件
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
-路由
re_path('^media/(?P<path>.*?)$', serve,kwargs={'document_root':settings.MEDIA_ROOT}),
-前端:
<img class="media-object" src="media/{{ article.blog.userinfo.avatar }}" alt="..." height="60px" width="60px">
个人站点路由设计
1 路由一定要放在最后
re_path('^(?P<username>\w+)$', views.personal_site),
个人站点页面设计
base.html#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
{% block title %}
{% endblock %}
</title>
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css">
{% block js %}
{% endblock %}
</head>
<body>
<div class="head" style="margin-bottom: 10px">
<div style="height: 120px;background-color: #1b6d85">
<div class="row">
<div class="col-md-2" style="font-size: 30px;color: white;margin: 20px">
{% block site_name %}
{% endblock %}
</div>
</div>
<div class="row">
<div class="pull-right" style="margin-right: 20px">
<span><a href="">后台管理</a></span>
<span>首页</span>
<span>订阅</span>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
{% block left %}
{% endblock %}
{% block main %}
{% endblock %}
</div>
</div>
</body>
</html>
site.html#
{% extends 'base.html' %}
{% block title %}
{{ user.username }}-博客园
{% endblock %}
{% block site_name %}
{{ user.blog.site_name }}
{% endblock %}
{% block left %}
<div class="col-md-2">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">我的标签</h3>
</div>
<div class="panel-body">
{% for tag in tag_list %}
<p><a href="">{{ tag.1 }}({{ tag.0 }})</a></p>
{% endfor %}
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">我的分类</h3>
</div>
<div class="panel-body">
{% for category in category_list %}
<p><a href="">{{ category.name }}({{ category.count }})</a></p>
{% endfor %}
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">随笔档案</h3>
</div>
<div class="panel-body">
{% for month in month_list %}
<p><a href="">{{ month.0|date:'Y年m月' }}({{ month.1 }})</a></p>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
{% block main %}
<div class="col-md-10">
<div class="article" style="margin-top: 20px">
{% for article in article_list %}
<div>
<h4 class="media-heading"><a href="">{{ article.title }}</a></h4>
<div class="media">
<div class="media-body">
{{ article.desc }}
</div>
<div style="margin-top: 10px" class="pull-right">
<span class="article_footer">posted @</span>
<span class="article_footer">{{ article.create_time|date:'Y-m-d H:i:s' }}</span>
{# <span class="glyphicon glyphicon-thumbs-up article_footer"> {{ article.up_num }}</span>#}
<span class="article_footer"><i
class="fa fa-comment-o fa-lg"></i> {{ article.up_num }}</span>
<span class="article_footer"><i
class="fa fa-home fa-lg"></i> {{ article.down_num }}</span>
<span class='article_footer'><i
class="fa fa-frown-o fa-lg"></i> {{ article.commit_num }}</span>
</div>
</div>
<hr>
</div>
{% endfor %}
</div>
</div>
{% 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-1 text-center" style="margin-top: 50px;margin-left: 100px">
<img src="/static/img/404.webp" alt="404图片">
</div>
<div class="col-md-10 col-md-offset-1 text-center" style='margin-top: 20px'>
<button class="btn btn-info btn-block"><a href="/index/">返回首页</a></button>
</div>
</div>
</div>
</body>
</html>
标签:count,项目,list,month,blog,user,article,BBS 来源: https://www.cnblogs.com/haiqinai/p/16053448.html