视频演示
基于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)