基于Django的Python小说网站 计算机毕业设计

视频演示

基于Django的Python小说网站 计算机毕业设计

前言

本小说网站让读者可以轻松地浏览各种题材的小说,从言情到科幻,从悬疑到历史,应有尽有。其强大的搜索功能和智能推荐系统,可以根据用户的阅读历史和喜好,精准地推荐符合其口味的作品,让阅读变得更加个性化和愉悦。而且,用户可以创建个人账户,收藏喜爱的作品,管理阅读进度,甚至参与讨论和评论,与其他书友交流心得体会,打造一个充满活力和互动的阅读社区。小说网站不仅仅是一个阅读平台,更是一个汇聚文学爱好者的精神家园,为用户提供了一个无限延伸的文学世界,让每一位读者都能在其中找到属于自己的阅读乐园。
主用技术:Python Django MySQL
项目运行环境:Python3.10+ MySQL8.0 Django


系统首页请添加图片描述

小说分类页面

请添加图片描述

小说详情

请添加图片描述

个人中心请添加图片描述

用户登录核心代码

class LoginView(View):
    @staticmethod
    def get(request):
        username = request.COOKIES.get('username')
        if username:
            return render(request, 'user/login.html', {'errmsg': '', 'username': username})
        return render(request, 'user/login.html', {'errmsg': '', 'username': ''})

    @staticmethod
    def post(request):
        username = request.POST.get('name')
        password = request.POST.get('pwd')
        cookie = request.POST.get('cookie')

        if not all([username, password]):
            return render(request, 'user/login.html', {'errmsg': '数据不完整'})

        user = authenticate(request, username=username, password=password)

        if not user:
            return render(request, 'user/login.html', {'errmsg': '用户名或密码错误'}

        login(request, user)
        next_url = request.GET.get('next', reverse('user:user', kwargs={'active': 'info'}))
        response = redirect(next_url)
        if cookie:
            response.set_cookie('username', username, max_age=14*24*3600)  # 缓存两星期
        else:
            response.delete_cookie('username')

        return response

小说评论核心代码

class CommentView(View):
    @staticmethod
    def post(request):
        content = request.POST.get('content')
        novel_id = request.POST.get('novel_id')

        if not content:
            return JsonResponse({'success': 0, 'errmsg': '内容不能为空'})

        novel = NovelsInfo.objects.get(id=novel_id)
        # 添加评论信息
        Comment.objects.create(
            content=content,
            user_info_id=request.user.id,
            novel_info_id=novel_id
        )
        # 将评论消息发给作者对应的账号
        Message.objects.create(
            title=f'{request.user.username}评论了你的作品,快看看吧',
            content=content,
            user_info_id=novel.author_info.user.id,
            message_type=1
        )

        novel.comments += 1
        novel.save()

        return JsonResponse({'success': 1, 'msg': '评论成功'})

浏览小说核心代码

class NovelsListView(View):
    @staticmethod
    def get(request, sort, page):
        if sort == 'all':
            novels_type = 'all'
            novels = NovelsInfo.objects.all()
        else:
            # 该类型的小说
            novels_type = NovelsType.objects.get(logo=sort)
            novels = NovelsInfo.objects.filter(novels_type_id=novels_type.id)
        # 热门小说
        hot_novels = NovelsInfo.objects.order_by('-watcher')[:3]
        # 小说类型
        novels_types = NovelsType.objects.all()

        paginator = MyPaginator(novels, 8)
        pages = paginator.page(page)
        pages.my_page_range = paginator.show_part_page_range(page, 8)

        context = {
            'pages': pages,
            'hot_novels': hot_novels,
            'novels_types': novels_types,
            'novels_type': novels_type,
            'sort': sort,
            'active': sort
        }
        return render(request, 'novels/list.html', context)
  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值