Commit ca8d19f6 by liuyingying

资源存储路径

parent 49e5cb4c
......@@ -90,54 +90,54 @@ def profile_submit(request):
def save_article(request):
userID = request.POST.get('userID', '')
title = request.POST.get('title', '')
content = request.POST.get('content', '')
editdatetime = datetime.datetime.now().date()
is_have_file = request.POST['isHaveFile']
link = ""
# 加入判断题目是否已存在
is_have = Articles.objects.filter(title=title).count()
if is_have > 0:
return JsonResponse({'message': 0, 'error': '该题目已存在'})
# 是否上传附件
if int(is_have_file) > 0:
myfile = request.FILES['myfile']
if Articles.objects.filter(attachment=myfile.name).count():
return JsonResponse({'message': 0, 'error': '文件已存在'})
else:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR, 'hunter', 'static', 'hunter/articles', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination, 'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
# 写入数据库
if Articles.objects.last() is None:
articleID = 1
userID = request.POST.get('userID', '')
title = request.POST.get('title', '')
content = request.POST.get('content', '')
editdatetime = datetime.datetime.now().date()
is_have_file = request.POST['isHaveFile']
link = ""
# 加入判断题目是否已存在
is_have = Articles.objects.filter(title=title).count()
if is_have > 0:
return JsonResponse({'message': 0, 'error': '该题目已存在'})
# 是否上传附件
if int(is_have_file) > 0:
myfile = request.FILES['myfile']
if Articles.objects.filter(attachment=myfile.name).count():
return JsonResponse({'message': 0, 'error': '文件已存在'})
else:
articleID = Articles.objects.last().articleID + 1
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR, 'static', 'articles', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination, 'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
# 判断作者是否存在
try:
authorID = User.objects.get(userID=userID)
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0, 'error': '用户不存在,请检查是否登录'})
return response
# 将文章写入数据库
try:
Articles.objects.create(articleID=articleID, authorID=userID, title=title, content=content,
editDateTime=editdatetime,
attachment=link)
response = JsonResponse({"message": 1})
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
# 写入数据库
if Articles.objects.last() is None:
articleID = 1
else:
articleID = Articles.objects.last().articleID + 1
# 判断作者是否存在
try:
authorID = User.objects.get(userID=userID)
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0, 'error': '用户不存在,请检查是否登录'})
return response
# 将文章写入数据库
try:
Articles.objects.create(articleID=articleID, authorID=userID, title=title, content=content,
editDateTime=editdatetime,
attachment=link)
response = JsonResponse({"message": 1})
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
return response
# 保存修改后的文章
......@@ -528,4 +528,4 @@ def add_sub_score(request):
return response
else:
response = JsonResponse({"message":"用户不存在"})
return response
\ No newline at end of file
return response
\ No newline at end of file
......@@ -220,49 +220,6 @@ def article_html(request, article_id):
# 写文章页面
def write_art_html(request):
if request.is_ajax():
userID = request.POST.get('userID', '')
title = request.POST.get('title', '')
content = request.POST.get('content', '')
editdatetime = datetime.datetime.now().date()
is_have_file = request.POST['isHaveFile']
link = ""
# 加入判断题目是否相同
is_have = Articles.objects.filter(title=title).count()
if is_have > 0:
return JsonResponse({'message': 0, 'error': '该题目已存在'})
if int(is_have_file) > 0:
myfile = request.FILES['myfile']
if Articles.objects.filter(attachment=myfile.name).count():
return JsonResponse({'message': 0, 'error': '文件已存在'})
else:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
destination = os.path.join(BASE_DIR, 'hunter', 'static', 'hunter/articles', myfile.name)
if os.path.exists(destination):
os.remove(destination)
with open(destination, 'wb+') as dest:
for chunk in myfile.chunks():
dest.write(chunk)
dest.close()
link = myfile.name
if Articles.objects.last() is None:
articleID = 1
else:
articleID = Articles.objects.last().articleID + 1
try:
authorID = User.objects.get(userID=userID)
except ObjectDoesNotExist as e:
response = JsonResponse({"message": 0, 'error': '用户不存在,请检查是否登录'})
return response
try:
Articles.objects.create(articleID=articleID, authorID=userID, title=title, content=content,
editDateTime=editdatetime,
attachment=link)
response = JsonResponse({"message": 1})
except ProgrammingError as e:
response = JsonResponse({"message": 0, 'error': '数据库错误'})
return response
return response
return render(request, 'write.html')
......@@ -277,7 +234,7 @@ def edit_art_html(request, article_id):
def uploadImg(request):
file_obj = request.FILES.get("image")
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
file_path = os.path.join(BASE_DIR, 'hunter', 'static', 'articles/images', file_obj.name)
file_path = os.path.join(BASE_DIR, 'static', 'articles/images', file_obj.name)
link_path = os.path.join('/static', 'articles/images', file_obj.name)
with open(file_path, 'wb') as f:
for chunk in file_obj.chunks():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment