def publish_article(request, cid=-1): if request.method == "POST": data = request.POST.dict() data.pop('csrfmiddlewaretoken') # data.pop('photo') file = request.FILES.get('photo', '') data.pop('photo') if file: savePath = os.path.join(settings.MDEIA_ROOT, file.name) print(savePath) with open(savePath, 'wb') as f: if file.multiple_chunks(): for myf in file.chunks(): f.write(myf) print('大于2.5') else: f.write(file.read()) print('小于2.5') data['picture'] = "/upload/" + file.name data['create_time'] = datetime.now() print(data, type(data)) Article.add(**data) # return HttpResponse('文件上传成功') return redirect(reverse("App:wenzhang_xinwen")) else: cid = cid if cid > 0 else Category.first_category().cid return render(request, 'wenzhang_xinwen_fabu.html', locals())
def publish_article(request, cid=-1): if request.method == "POST": data = request.POST.dict() data.pop('csrfmiddlewaretoken') data.pop('photo') Article.add(**data) return redirect(reverse("App:main")) else: cid = cid if cid > 0 else Category.first_category().cid return render(request, 'wenzhang_xinwen_fabu.html', locals())
def update_article(request, cid, aid): article = Article.objects.get(pk=aid) tag = Tag.objects.filter(aid=aid) if request.method == "POST": data = request.POST.dict() data.pop('csrfmiddlewaretoken') print("woshishui") # data.pop('photo') file = request.FILES.get('picture', '') print(file) if file: savePath = os.path.join(settings.MDEIA_ROOT, file.name) print(savePath) with open(savePath, 'wb') as f: if file.multiple_chunks(): for myf in file.chunks(): f.write(myf) print('大于2.5') else: f.write(file.read()) print('小于2.5') data['picture'] = "/static/upload/" + file.name data['create_time'] = datetime.now() data['aid'] = article.aid print(data) article.delete() Article.add(**data) # return HttpResponse('文件上传成功') return redirect(reverse("App:wenzhang_xinwen")) else: s = '' for t in tag: s += t.name s += "," s = s[:-1] print(s) print(tag) return render(request, 'wenzhang_xinwen_info.html', locals())
def fabu(request, cid=-1): if request.method == 'POST': fobj = request.FILES.get('picture') path = settings.MDEIA_ROOT fp = FileUpload(fobj) # print(fp) #<App02.utils.FileUpload object at 0x00000000048BA4A8> # print(fobj.name) #2.png form = ArticleForm(request.POST) if form.is_valid() and fp.upload(path): data = request.POST.dict() print(data) data.pop('csrfmiddlewaretoken') data['picture'] = 'upload/' + fobj.name # print(data) Article.add(**data) messages.success(request, '发表成功') return redirect(reverse('App:main')) # return HttpResponse('ok') else: messages.error(request, '文件上传有误,请重新上传') return render(request, 'wenzhang_xinwen_fabu.html', locals()) else: cid = cid if cid > 0 else Category.first_category().cid return render(request, 'wenzhang_xinwen_fabu.html', locals())