基于django的个人博客网站建立(五)
程序员文章站
2022-04-08 14:45:48
基于django的个人博客网站建立(五) 前言 之前鸽了两天,今天继续再写点 主要内容 今天加了个展示照片的功能,就叫他生活记录吧 先建表 class Record(models.Model): title = models.CharField(max_length=128) content = m ......
基于django的个人博客网站建立(五)
前言
之前鸽了两天,今天继续再写点
主要内容
今天加了个展示照片的功能,就叫他生活记录吧
先建表
class record(models.model): title = models.charfield(max_length=128) content = models.textfield() picture = models.charfield(max_length=128) creationtime = models.datetimefield(auto_now_add=true)
主要功能就是为了上传一张图片,并添加标题,内容来记录一些有趣的事情
于是在后台要添加图片的上传
借用了别人点击上传图片后显示图片的代码
<input type="file" id="chooseimage" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" name="picture" class="form-control" aria-invalid="false"> <!-- 保存用户自定义的背景图片 --> <img id="cropedbigimg" value='custom' alt="请添加图片" data-address='' title="我的图片"/> <script> $('#chooseimage').on('change',function(){ var filepath = $(this).val(), //获取到input的value,里面是文件的路径 fileformat = filepath.substring(filepath.lastindexof(".")).tolowercase() src = window.url.createobjecturl(this.files[0]); //转成可以在本地预览的格式 // 检查是否是图片 if( !fileformat.match(/.png|.jpg|.jpeg/) ) { alert('请选择图片'); return; } $('#cropedbigimg').attr('src',src); }); </script>
效果为:
处理的视图函数为:
@auth def publish_record(request): if request.method == 'get': return render(request, 'backend/publish_record.html') if request.method == 'post': if request.files: myfile = none for i in request.files: myfile = request.files[i] if myfile: dir = os.path.join( os.path.join( os.path.join( os.path.join( base_dir, 'statics'),'assets'),'images'), 'record') destination = open(os.path.join(dir, myfile.name), 'wb+') for chunk in myfile.chunks(): destination.write(chunk) destination.close() title = request.post.get('title') content = request.post.get('content') models.record.objects.create(title=title,content=content,picture=myfile.name) else: messages.error(request,'输入信息有误') return redirect('/backend/publish_record')
主要是把图片放到一个固定的文件夹下,之后在页面显示时路径的前缀是固定的
在前端显示为
接下来设置默认的访问错误页面:
在url.py中添加
handler403 = views.permission_denied handler404 = views.page_not_found handler500 = views.page_error
然后编写对应的视图处理函数
def permission_denied(request): return render(request,'show/403.html') def page_not_found(request): return render(request, 'show/404.html') def page_error(request): return render(request, 'show/500.html')
然后随意访问一个不存在的url:
最后加一个关于界面,也就是一个简单的介绍,内容也用markdown编辑
首先后台添加一个关于内容的输入页面
在后台通过.md文件的方式存储内容
def about_edit(request): if request.method == 'get': dir = os.path.join( os.path.join( os.path.join( os.path.join( base_dir, 'statics'), 'assets'), 'about'), 'about.md') with open(dir,'r+') as f: content = f.read() return render(request,'backend/about_edit.html',{'content':content}) if request.method == 'post': dir = os.path.join( os.path.join( os.path.join( os.path.join( base_dir, 'statics'), 'assets'), 'about'), 'about.md') content = request.post.get('content') with open(dir,'w+') as f: f.write(content) return redirect('/backend/about_edit')
这样就可以在前端显示关于页面的内容了
def about(request): if request.method == 'get': all_type = models.articletype.objects.all() dir = os.path.join( os.path.join( os.path.join( os.path.join( base_dir, 'statics'), 'assets'), 'about'), 'about.md') with open(dir, 'r+') as f: content = f.read() content = markdown(content) return render(request, 'show/about.html', {'all_type': all_type,'content':content })
总结
基本内容已经完成,今天刚搁腾讯云那提交了网站备案审核,明天预计把项目先部署到服务器上看看效果
上一篇: 常用文件转码工具汇总
推荐阅读
-
基于django的个人博客网站建立(六)
-
基于django的个人博客网站建立(一)
-
基于django的个人博客网站建立(五)
-
基于django的个人博客网站建立(二)
-
今天学了一下午,我就建立了自己第一个云上网站,阿里云的这个课程挺NB的,并且还免费,技能超级实用啊,不但有个人博客、论坛、还有电商网站的建设,今天我就NB哄哄建 microsoft
-
基于django的个人博客网站建立(七)
-
基于django的个人博客网站建立(三)
-
基于django的个人博客网站建立(六)
-
基于django的个人博客网站建立(一)
-
建立了自己第一个云上网站,阿里云的这个课程挺NB的,并且还免费,技能超级实用啊,不但有个人博客、论坛、还有电商网站的建设,今天我就NB哄哄建了一个自己的博客 microsoftwordpress