BBS 项目(五)


目录
  • BBS 项目(五)
    • 实现伪静态(人工智能版)
      • 路由整合
      • 视图函数
    • 左侧标签,分类,归档写成inclusion_tag
      • left.py
      • left.html
      • 使用
    • 文章详情页面搭建
    • 文章点赞点踩样式
    • 文章点赞点踩后端
      • 后端
      • 前端

实现伪静态(人工智能版)

路由整合

from django import template
register = template.Library()
from blog import models
from django.db.models.functions import TruncMonth
from django.db.models import Count

@register.inclusion_tag('left.html')
def left_inclusion_tag(username):
    user=models.UserInfo.objects.filter(username=username).first()
    category_list = models.Category.objects.filter(blog=user.blog).annotate(count=Count('article')).values('count',
                                                                                                           'name', 'id')
    tag_list = models.Tag.objects.filter(blog=user.blog).annotate(count=Count('article')).values_list('count',
                                                                                                      'name', 'id')

    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 {'username':username,'category_list':category_list,'tag_list':tag_list,'month_list':month_list}

left.html

def upanddown(request):
    if request.is_ajax():
        response = {'status': 100, 'msg': None}
        if request.user.is_authenticated:
            article_id = request.POST.get('article_id')
            is_up = json.loads(request.POST.get('is_up'))
            res = models.UpAndDown.objects.filter(article_id=article_id, user=request.user)
            if res:
                # 点过了
                response['status'] = 102
                response['msg'] = '你已经点过了'
                return JsonResponse(response)
            # 事务操作
            with transaction.atomic():
                if is_up:
                    models.Article.objects.filter(id=article_id).update(up_num=F('up_num') + 1)
                    response['msg'] = '点赞成功'
                else:
                    models.Article.objects.filter(id=article_id).update(down_num=F('down_num') + 1)
                    response['msg'] = '点踩成功'

                models.UpAndDown.objects.create(user=request.user, article_id=article_id, is_up=is_up)
            return JsonResponse(response)
        else:
            response['status'] = '101'
            response['msg'] = '请去登录'
            return JsonResponse(response)

前端

   $('.action').click(function () {
        var is_up=$(this).hasClass('diggit')
        var span= $(this).children('span')
        $.ajax({
            url:'/upanddown/',
            method:'post',
            //谁(当前登录用户)对那篇文章点赞或点踩
            data:{article_id:'{{ article.id }}',is_up:is_up,'csrfmiddlewaretoken':'{{ csrf_token }}'},
            success:function (data) {
                console.log(data)

                $('#digg_tips').html(data.msg)
                if (data.status=='100'){
                    //let num=Number(span.html())+1
                    span.html(Number(span.html())+1)
                }

            }
        })

    })
BBS