写文章

This commit is contained in:
gjj 2021-12-31 18:24:50 +08:00
parent d77c898343
commit e3689d9217
4 changed files with 51 additions and 2 deletions

View File

@ -5131,3 +5131,10 @@ INFO 2021-12-31 18:15:05,924 basehttp 124 "GET /static/common/common.css HTTP/1.
INFO 2021-12-31 18:15:05,927 basehttp 124 "GET /static/common/zhan.css HTTP/1.1" 304 0 INFO 2021-12-31 18:15:05,927 basehttp 124 "GET /static/common/zhan.css HTTP/1.1" 304 0
INFO 2021-12-31 18:15:05,928 basehttp 124 "GET /static/js/index.js HTTP/1.1" 304 0 INFO 2021-12-31 18:15:05,928 basehttp 124 "GET /static/js/index.js HTTP/1.1" 304 0
INFO 2021-12-31 18:15:09,666 basehttp 124 "GET /writeblog/ HTTP/1.1" 200 6337 INFO 2021-12-31 18:15:09,666 basehttp 124 "GET /writeblog/ HTTP/1.1" 200 6337
INFO 2021-12-31 18:20:18,808 basehttp 124 "GET / HTTP/1.1" 200 5158
INFO 2021-12-31 18:20:18,856 basehttp 124 "GET /static/common/zhan.css HTTP/1.1" 304 0
INFO 2021-12-31 18:20:18,858 basehttp 124 "GET /static/js/common.js HTTP/1.1" 304 0
INFO 2021-12-31 18:20:20,405 basehttp 124 "GET /writeblog/ HTTP/1.1" 200 6337
INFO 2021-12-31 18:20:32,515 basehttp 124 "POST /writeblog/ HTTP/1.1" 302 0
INFO 2021-12-31 18:20:32,519 basehttp 124 "GET / HTTP/1.1" 200 5158
INFO 2021-12-31 18:20:32,537 basehttp 124 "GET /static/js/index.js HTTP/1.1" 304 0

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

View File

@ -118,7 +118,10 @@ class WriteBlogView(View):
def get(self,request): def get(self,request):
return render(request,'write_blog.html') return render(request,'write_blog.html')
from home.models import ArticleCategory from home.models import ArticleCategory,Article
import logging
#创建获取日志器
logger=logging.getLogger('django')
class WriteBlogView(LoginRequiredMixin,View): class WriteBlogView(LoginRequiredMixin,View):
def get(self,request): def get(self,request):
@ -128,4 +131,43 @@ class WriteBlogView(LoginRequiredMixin,View):
context = { context = {
'categories': categories 'categories': categories
} }
return render(request,'write_blog.html',context=context) return render(request,'write_blog.html',context=context)
def post(self, request):
# 接收数据
avatar = request.FILES.get('avatar')
title = request.POST.get('title')
category_id = request.POST.get('category')
tags = request.POST.get('tags')
sumary = request.POST.get('sumary')
content = request.POST.get('content')
user = request.user
# 验证数据是否齐全
if not all([avatar, title, category_id, sumary, content]):
return HttpResponseBadRequest('参数不全')
# 判断文章分类id数据是否正确
try:
article_category = ArticleCategory.objects.get(id=category_id)
except ArticleCategory.DoesNotExist:
return HttpResponseBadRequest('没有此分类信息')
# 保存到数据库
try:
article = Article.objects.create(
author=user,
avatar=avatar,
category=article_category,
tags=tags,
title=title,
sumary=sumary,
content=content
)
except Exception as e:
logger.error(e)
return HttpResponseBadRequest('发布失败,请稍后再试')
# 返回响应,跳转到文章详情页面
# 暂时先跳转到首页
return redirect(reverse('home:index'))