# 个人网站 **Repository Path**: overload__hcf/personal-website ## Basic Information - **Project Name**: 个人网站 - **Description**: 基于python Django开发的个人网站(个人博客) - **Primary Language**: Python - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 9 - **Forks**: 1 - **Created**: 2021-05-15 - **Last Updated**: 2024-11-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 基于python Django开发的个人网站(个人博客) > ### 项目简介 使用python的django框架开发个人网站,实现个人的简历以及个人博客开发 演示地址:http://120.25.144.123:8000/ #### app划分 - > myblog: 个人博客app - > IntroductionDetail:个人简历app #### 前端模板 [个人介绍页]: https://gitee.com/honglanbaoshi/sherryj " " [博客页面]: https://gitee.com/lanxuewei/GeRenBoKeQianDuanJingTaiJieMian/tree/master/myBlog > ### 项目部署 - 开发环境 > python 3.7 > > django 3.1.7 - 环境安装 ```bash pip install django # 安装django pip install markdown # 博客显示风格 pip install smtplib # 邮件服务器 pip install email # 邮件文本 pip install Pygments # markdown代码高亮显示 ``` - 项目启动 进入项目根目录,执行 ```bash python manage.py runserver ``` > ### 模块开发 - 首页 > 运行截图 ![image-20210519085427811](.\项目截图\image-20210519085427811.png) ![image-20210519085529630](项目截图\image-20210519085529630.png) ![image-20210519090106769](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519090106769.png) > 实现 ```python 路由跳转:urls.py path('', views.index,name='index') 处理函数:views.py def index(request): return render(request,'index.html') ``` 首页介绍都是采用的静态数据,并不从后台取,所以前端代码就不展示了. - 简历模块(在首页点击了解更多进入该模块) > 运行截图 ![image-20210519091810253](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519091810253.png) ![image-20210519091819703](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519091819703.png) ![image-20210519091833774](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519091833774.png) ![image-20210519091849877](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519091849877.png) ![image-20210519091901616](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519091901616.png) > 实现 首先创建数据库model ```python # 关于 class AboutMe(models.Model): #技能 class Skills(models.Model): #项目或经历 class Experience(models.Model): # 创建完执行命令 - python manage.py makegiration - python manage.py migrate ``` 在admin.py中注册model ```python from django.apps import apps models = apps.get_models() for model in models: try : admin.site.register(model) except admin.sites.AlreadyRegistered: pass ``` 打开后台管理网站:localhost:8080/admin 进行表格数据的增加 路由urls.py ```python path('introduction/', views.introduction,name='introduction') ``` 处理函数 ```python def introduction(request): active = '' #活动菜单 msg = [] #返回消息,第一个值为是否成功,第二个为信息 # 从数据库取得数据 aboutMes = AboutMe.objects.all() experiences = Experience.objects.all().filter(type='经历') projects = Experience.objects.all().filter(type='项目') skills = Skills.objects.all() if request.method == 'POST': active = 'relation' title = request.POST.get('title') content = request.POST.get('content') sender = '2929015513@qq.com' #发送方 设成自己邮箱 recver = '2929015513@qq.com' #接受方 设成自己邮箱 pwd = '****' #smtp授权码 改成自己的 # content 发送内容 "plain"文本格式 utf-8 编码格式 message = MIMEText(content, "plain", "utf-8") message['Subject'] = title # 邮件标题 message['To'] = recver # 收件人 message['From'] = sender # 发件人 try: smtp = smtplib.SMTP_SSL("smtp.qq.com",465) # 实例化smtp服务器 smtp.login(sender,pwd) # 发件人登录 # as_string 对 message 的消息进行了封装 smtp.sendmail(sender, [recver], message.as_string()) smtp.close() msg = ['1','发送成功'] except: msg = ['0','网络错误,发送失败!'] return render(request, 'introduction.html',locals()) ``` 正常访问简历页面时会从数据库中取得个人信息,并传回前台进行展示 ![](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519093508447.png) 当点击发送邮件按钮时,执行request.method == 'POST'的分支,这时会通过smtp授权码和发送者账号进行登录邮件服务器,并封装好邮件发送给接收者,我这里把发送者和接收者都设为同一个人. - 博客模块 > 运行截图 博客首页 ![image-20210519094920714](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519094920714.png) 博客内容 ![image-20210519095056653](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519095056653.png) 留言 ![image-20210519095222870](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519095222870.png) 照片墙 ![image-20210519095254526](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519095254526.png) 爱好 ![image-20210519095324695](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519095324695.png) 书籍 ![image-20210519095341124](C:\Users\涛涛\AppData\Roaming\Typora\typora-user-images\image-20210519095341124.png) > 实现 先创建model ```python #相册 class photo(models.Model): #博客 class Article(models.Model): # 博客封面(与博客一对一) class cover(models.Model): #留言 class message(models.Model): ``` 也是在后台网站进行数据增删改 路由 ```python path('myblog/', include('myblog.urls'),name='myblog') -- myblog.urls path('blogindex/',views.index,name='blogindex'),#博客首页 path('detail/',views.detail,name='detail'),#博客详细 path('jump//',views.jump,name='jump'),#跳转页面 path('message/',views.addMsg,name='message'),#添加留言 ``` 处理函数 ```python #首页 获取博客列表 def index(request): #跳转页面 根据pageName跳转页面,并获取对应页面需要的数据 def jump(request,pageName): #博客详细页 首先根据id查询出对应的博客,并将博客内容markdown格式化 def detail(request,id): article = get_object_or_404(Article,id=id) article.read_count += 1 article.save() article.content = markdown( article.content.replace("\r\n",' \n'), extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite',#语法高亮拓展 'markdown.extensions.toc'#自动生成目录 ])#article.content内容为html return render(request,'blogDetail.html',locals()) #添加留言 def addMsg(request): ``` 文章内容在前台要以markdown格式显示还要加入safe过滤器 ```python {{article.content|safe}} ```