<h3>个人站点下的</h3>
知识点
url (r'(?P<username>w+)/p/(?P<id>d+)', xiangxi,name='xxx'),#文章详情页面 url (r'dianzhan/', dianzhan), #文章详情中点赞的一个接收ajax请求路由 url (r'pinlun/', pinlun), #文章详情中评论的一个接收ajax请求路由 url (r'(?P<username>w+)/(?P<n>a|b|c)/(?P<d>.*)', zhandian.as_view ()),#个人站点页面 分类页面 url (r'(?P<username>w+)/', zhandian.as_view ()), #个人站点页面
1.样式
a.css
2.所有文章
class zhandian(View): def get(self,request,username,*args,**kwargs): username = username user = models.User_info.objects.filter (username=username).first () blog = user.blog wen=models.Wenzhang.objects.filter(blog=blog)
3.分类下的文章
人站点下的所有文章进行再次过滤
4.文章详情
#略
5.文章评论点赞
#点赞注意问题:1 事物 2 一个用户只能点赞一次unique_together=('user_info','wenzhang')
def dianzhan(request): dic = {'status': 100, 'msg': ''} if request.user.is_authenticated (): id = request.POST.get ('id') x = models.Dianzan.objects.filter (user_info=request.user, wenzhang_id=id) if x: dic = {'status': 101, 'msg': '该用户已点击过'} else: is_up = request.POST.get ('is_up') import json is_up = json.loads (is_up) from django.db import transaction with transaction.atomic (): models.Dianzan.objects.create (user_info=request.user, wenzhang_id=id, is_up=is_up) # models.Dianzan.objects.filter(is_up=True).values('wenzhang').annotate(c=Count('wenzhang')).values('wenzhang','c') # models.Dianzan.objects.filter(is_up=False).values('wenzhang').annotate(c=Count('wenzhang')).values('wenzhang','c') if is_up == True: models.Wenzhang.objects.filter (id=id).update (upnum=F ('upnum') + 1) dic['msg'] = '点赞成功' else: models.Wenzhang.objects.filter (id=id).update (downnum=F ('downnum') - 1) dic['msg'] = '点踩成功' else: dic['status'] = 101 dic['msg'] = '请先登录' print (dic) return JsonResponse (dic)
#评论注意问题:1 安全的html文本通过过滤器 safe 可以不转义 显示优雅的页面 2 评论中的回复评论
#略
6.文章评论点赞统计
数据库优化:把这个连表查询得到的数据,直接写入文章表中,每次浏览器显示的数据直接从文章表中读取
class Wenzhang (models.Model): id = models.AutoField (primary_key=True) title = models.CharField (max_length=32) category = models.ForeignKey (to='Category', to_field='id', null=True, blank=True) blog = models.ForeignKey (to='Blog', to_field='id', null=True, blank=True) content = models.TextField () desc = models.CharField (max_length=300) create_time = models.DateTimeField (auto_now_add=True) tag = models.ManyToManyField (to='Tag', through='Wen_Tag', through_fields=('wenzhang', 'tag')) upnum=models.IntegerField(default=0) downnum=models.IntegerField(default=0) pinnum = models.IntegerField (default=0)
#往关联表中添加一条评论点赞数 通过事物文章表中更新增加1(用到F函数)