示例展示
http://www.guanacossj.com/JiaBlog/mylist/
这个页面需要的数据主要是echarts的和用户写的文章
首先看echarts:
后端实现:
@check_login
def mylist(request):
# if request.method == 'GET':
change_info(request)
name = request.session.get('user_name')
username = name
blog_list = Articles.objects.filter(status="有效").filter(authorname__name__exact=username).order_by(
"-timestamp") # 获取所有数据
paginator = Paginator(blog_list, 10) # 分页,每页10条数据
page = request.GET.get('page')
try:
contacts = paginator.page(page) # contacts为Page对象!
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
blog_list_views = Articles.objects.filter(status="有效").order_by('-views')[0:10]
blog_list_greats = Articles.objects.filter(status="有效").order_by("-greats")[0:10]
blog_list_comments = Articles.objects.filter(status="有效").order_by("-comments")[0:10]
blog_list_news = Articles.objects.filter(status="有效").order_by("-timestamp")[0:10] # 获取10 recent posts
comments = Comment.objects.count()
tags = Tag.objects.all()
view = []
count = Articles.objects.count()
categorys = Category.objects.all()
catcharts = {}
for cats in categorys:
catnums = Articles.objects.filter(category=cats.id).filter(status='有效').filter(
authorname__name__exact=username).count()
if catnums != 0:
catcharts[cats.name] = catnums
else:
continue
user = BlogUser.objects.get(name=name)
mycategorys = Category.objects.filter(
id__in=set([item[key] for item in blog_list.values('category') for key in item])).values('name')
mycategorys = list([item[key] for item in mycategorys for key in item])
django = Articles.objects.filter(category=1).filter(status='有效').filter(authorname__name__exact=username).count()
python = Articles.objects.filter(category=2).filter(status='有效').filter(authorname__name__exact=username).count()
leetcode = Articles.objects.filter(category=3).filter(status='有效').filter(authorname__name__exact=username).count()
mysql = Articles.objects.filter(category=4).filter(status='有效').filter(authorname__name__exact=username).count()
other = Articles.objects.filter(category=5).filter(status='有效').filter(authorname__name__exact=username).count()
java = Articles.objects.filter(category=6).filter(status='有效').filter(authorname__name__exact=username).count()
javascript = Articles.objects.filter(category=7).filter(status='有效').filter(
authorname__name__exact=username).count()
categorynum = [django, python, leetcode, mysql, other, java, javascript]
categorydic = dict(zip(mycategorys, categorynum))
try:
version = models.Version.objects.filter(version_time=datetime.datetime.now().strftime('%Y-%m-%d')).values(
'version_content')
versions = [item[key] for item in version for key in item][0].split(";")
except IndexError as err:
print("error", err)
version = models.Version.objects.order_by('-version_time')[0:1].values('version_content')
versions = [item[key] for item in version for key in item][0].split(";")
else:
version = models.Version.objects.all()[0:1].values('version_content')
versions = [item[key] for item in version for key in item][0].split(";")
context = {
'blog_list': blog_list,
'blog_list_views': blog_list_views,
'blog_list_comments': blog_list_comments,
'tags': tags,
'contacts': contacts,
'blog_list_greats': blog_list_greats,
'blog_list_three': blog_list_news,
'comments': comments,
'categorys': categorys,
'versions': versions,
'user': user,
'mycategorys': mycategorys,
# 'mycategory':mycategory,
'django': django,
'python': python,
'mysql': mysql,
'leetcode': leetcode,
'other': other,
'java': java,
'javascript': javascript,
'categorydic': categorydic,
'catcharts': catcharts,
}
# print((maxarticle))
return render(request, 'editorlist.html', context=context)
用装饰器检查登录状态,然后获取当前登录用户的用户名
下面看数据库表结构设计
class Articles(models.Model):
id = models.AutoField(primary_key=True) # id
title = models.CharField(max_length=150) # 博客标题
body = models.TextField() # 博客正文
timestamp = models.DateTimeField() # 创建时间
authorname = models.ForeignKey('JiaBlog.BlogUser', on_delete=models.CASCADE) # 作者姓名
views = models.PositiveIntegerField(default=0)
category = models.ForeignKey(Category, on_delete=models.CASCADE, primary_key=False)
tags = models.ManyToManyField(Tag, blank=True, null=True)
greats = models.PositiveIntegerField(default=0)
comments = models.IntegerField(default=0)
status = models.CharField(max_length=20, default="DEL")
brief = models.CharField(max_length=200, blank=True, null=True)
pic = models.ImageField(upload_to='jiablogimages')
bodypic = models.ImageField(upload_to='jiablogimages', blank=True, null=True)
istop = models.CharField(max_length=5, default='', null=True, blank=True)
articlebodybrief = models.TextField(blank=True, null=True)
由于authorname 是外键扩展,所以注意使用拿到的用户筛选博客作者的用法
name = request.session.get('user_name')
blog_list = Articles.objects.filter(status="有效").filter(authorname__name__exact=name).order_by("-timestamp")
然后是使用django自带的分页器
paginator = Paginator(blog_list, 10) # 分页,每页10条数据
page = request.GET.get('page')
try:
contacts = paginator.page(page) # contacts为Page对象!
except PageNotAnInteger:
# If page is not an integer, deliver first page.
contacts = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
contacts = paginator.page(paginator.num_pages)
接下来是按照分类统计文章数,待续...