Django-based personal blog site build (five)
Foreword
Pigeon two days before, we continue to write point today
main content
Today added a photo show feature, called it his life record
First built table
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)
The main function is to upload an image, and add a title, content to record something interesting
So in the background to add a picture to upload
Borrow someone else click upload pictures show the picture code
<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>
Effect:
Treatment is a function of view:
@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')
The main picture is put into a fixed folder, and then when the page is displayed prefix path is fixed
In the front-end display is
Next, set the default access error page:
Add in the url.py
handler403 = views.permission_denied
handler404 = views.page_not_found
handler500 = views.page_error
Then write the corresponding view handler
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')
Then feel free to visit a non-existent url:
Finally, add on a screen, which is a brief introduction, the content also with markdown editor
First, the background add an input on the content of the page
In the background .md file stored content
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')
This will display the contents on the front page of the
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 })
to sum up
The basic contents has been completed today just put Tencent cloud that submitted the site for the record audit, the project is expected to be deployed tomorrow to see the effect on the server