model.py代码段
class Blogs(models.Model):
blogID = models.AutoField(primary_key=True)
# 此处省略你的其他字段信息
# 阅读量自增的方法,在view模块直接调用,默认的阅读量为0
readCount = models.PositiveIntegerField(default=0)
def increase_read(self):
self.readCount += 1
self.save(update_fields=['readCount'])
class Meta:
db_table = "Blogs"
views.py代码段
blogs_obj = Blogs.objects.get(blogID=blogID, statusID=1)
# 每次访问该页面时,会调用一次该方法,自增1
# read count
blogs_obj.increase_read()
return render(request, "viewBlogs.html", {'readCount': blogs_obj.readCount,})
HTML代码段
阅读: {{ readCount }}
效果展示
留言与评论(共有 0 条评论) “” |