发布时间:2025-06-24 17:46:53 作者:北方职教升学中心 阅读量:783
本论文旨在基于Python的Django框架开发一个简单而功能全面的博客系统,并利用MySQL数据库进行数据存储。
随着博客系统的不断发展与变化,如何构建一个高效、近年来,博客系统逐渐发展成为内容管理系统(CMS)中的一种重要应用形式,其在网站开发、
在开发Django博客系统时,数据存储是不可忽视的关键部分。评论互动、MySQL作为一种开源的关系型数据库管理系统,凭借其高效的数据存储和查询能力,已经成为Web开发中广泛使用的数据库之一。该系统旨在提供博客发布、
系统展示
部分代码
import datetimefrom django.core.paginator import Paginator, PageNotAnInteger, EmptyPagefrom django.db.models import F, Qfrom django.shortcuts import render, redirectfrom django.urls import reversefrom article.models import Article, Commentfrom user.models import MyUser# Create your views here.def article(request, id, page, typeId): """ 根据用户id和页码查询帖子 :param request: :param id: :param page: :param typeId: 0表示查询全部 :return: """ pageSize = 10 # 每页大小 user = MyUser.objects.filter(id=id).first() if not user: return redirect(reverse('toRegisterPage')) if typeId == None or typeId == 0: articleList = Article.objects.filter(author_id=id).order_by('-create_time') else: articleList = Article.objects.filter(author_id=id, type_id=typeId).order_by('-create_time') paginator = Paginator(articleList, pageSize) try: pageData = paginator.page(page) # 获取一页数据 except PageNotAnInteger: pageData = paginator.page(1) # 如果前端传来的页码不是整型,则返回第一页数据 except EmptyPage: pageData = paginator.page(paginator.num_pages) # 如果前端传来的页码超过实际页数,则返回最后一页数据 return render(request, 'article.html', locals())def detail(request, id, aId): """ 根据用户id和帖子id查看详细信息 & 添加评论信息 :param request: :param id: :param aId: :return: """ if request.method == 'GET': # 查询帖子信息 user = MyUser.objects.filter(id=id).first() article = Article.objects.filter(id=aId).first() # 阅读量加1 Article.objects.filter(id=aId).update(reads=F('reads') + 1) # 获取博客评论信息 commentList = Comment.objects.filter(article_id=aId).order_by('-create_time') return render(request, 'detail.html', locals()) else: # 添加评论信息 user = request.POST.get("user") content = request.POST.get("content") value = {'user': user, 'content': content, 'article_id': aId, 'create_time': datetime.datetime.now(), 'author_id': id} Comment.objects.create(**value) kwargs = {'id': id, 'aId': aId} return redirect(reverse('detail', kwargs=kwargs))def search(request, id): """ 根据搜索条件搜索指定用户帖子,只显示前10条记录 :param request: :param id: :param v: :return: """ v = request.POST.get("v") articleList = Article.objects.filter(Q(author_id=id, title__contains=v) | Q(author_id=id, content__contains=v)) paginator = Paginator(articleList, 10) pageData = paginator.page(1) return render(request, 'result.html', locals())
<!DOCTYPE html><html lang="en"><head> {% load static %} <title>博客系统用户登录界面-Powered by python222</title> <script src="{% static "js/jquery-1.11.2.min.js" %}"></script> <link rel="stylesheet" href="{% static "css/login.css" %}" type="text/css"> <script type="text/javascript"> $(function () { //得到焦点 $("#password").focus(function () { $("#left_hand").animate({ left: "150", top: " -38" }, { step: function () { if (parseInt($("#left_hand").css("left")) > 140) { $("#left_hand").attr("class", "left_hand"); } } }, 2000); $("#right_hand").animate({ right: "-64", top: "-38px" }, { step: function () { if (parseInt($("#right_hand").css("right")) > -70) { $("#right_hand").attr("class", "right_hand"); } } }, 2000); }); //失去焦点 $("#password").blur(function () { $("#left_hand").attr("class", "initial_left_hand"); $("#left_hand").attr("style", "left:100px;top:-12px;"); $("#right_hand").attr("class", "initial_right_hand"); $("#right_hand").attr("style", "right:-112px;top:-12px"); }); }); function checkForm() { var username = $("#username").val(); var password = $("#password").val(); if (username == null || username == "") { $("#error").html("用户名不能为空!"); return false; } if (password == null || password == "") { $("#error").html("密码不能为空!"); return false; } return true; } </script></head><body><DIV class="top_div"></DIV><form action="login" method="post" onsubmit="return checkForm()"> {% csrf_token %} <DIV style="background: rgb(255, 255, 255); margin: -100px auto auto; border: 1px solid rgb(231, 231, 231); border-image: none; width: 400px; height: 230px; text-align: center;"> <DIV style="width: 165px; height: 96px; position: absolute;"> <DIV class="tou"> </DIV> <DIV class="initial_left_hand" id="left_hand"> </DIV> <DIV class="initial_right_hand" id="right_hand"> </DIV> </DIV> <P style="padding: 30px 0px 10px; position: relative;"> <SPAN class="u_logo"></SPAN> <INPUT id="username" name="username" class="ipt" type="text" placeholder="请输入用户名" value="{ { username }}"> </P> <P style="position: relative;"> <SPAN class="p_logo"></SPAN> <INPUT id="password" name="password" class="ipt" type="password" placeholder="请输入密码" value="{ { password }}"> </P> <DIV style="height: 50px; line-height: 50px; margin-top: 30px; border-top-color: rgb(231, 231, 231); border-top-width: 1px; border-top-style: solid;"> <P style="margin: 0px 35px 20px 45px;"> <SPAN style="float: left;">Python222开源博客系统 <a href="register.html" style="color: darkcyan">>>用户注册</a></SPAN> <SPAN style="float: right;"> <input type="submit" style="background: rgb(0, 142, 173); padding: 7px 10px; border-radius: 4px; border: 1px solid rgb(26, 117, 152); border-image: none; color: rgb(255, 255, 255); font-weight: bold;" value="登录"/> </SPAN> </P> </DIV> <span style="padding-top: 5px"><font color="red" id="error">{ { errorinfo }}</font></span> </DIV></form><div style="text-align:center;padding-top: 30px"> </div></body></html>
源码代码
链接:https://pan.baidu.com/s/1zz7oqInJcMZeZ6e_pAaEvA
提取码:1234