文章点赞、浏览量
点赞和浏览量其实就是牵扯一个自加,当新打开文章或者刷新页面都要自加
所以,明白这个原理就好办了:
创建字段
total_count = models.PositiveIntegerField(default=0)
后端逻辑
def get_count(request, id):
new = News.objects.get(id=id)
new.total_count += 1
new.save(update_fields=['total_count'])
content = {}
content['count'] = new
return content
前端展示
<div class="one">
<div class="count">浏览量:{{ count.total_count }}</div>
</div>