def wenzhang_xinwen_fabu(request): if request.session.get('username'): categorise = Category.objects.all() fobj = request.FILES.get('photo') path = settings.MDEIA_ROOT fp = FileUpload(fobj) path_photo = os.path.join('/static/img/' + str(fobj)) if request.method == 'POST': #获取表单提交的信息 cate = request.POST.get('cate') c_id = Category.objects.filter(c_name=cate).first().c_id a_title = request.POST.get('title') a_content = request.POST.get('content') a_time = datetime.now() if fp.Upload(path): print(path_photo) if a_title and a_content: article = Article(a_title=a_title, a_content=a_content, a_create_time=a_time, c_id=c_id, a_picture=path_photo) article.save() else: return redirect(reverse('App:login')) return render(request, 'wenzhang_xinwen_fabu.html', locals())
def many_to_many(): # 文章 article1 = Article(title='金瓶梅') article2 = Article(title='少年阿宾') article3 = Article(title='我的兄弟叫顺溜') # 章节 tag1 = Tag(name='第一章第一回') tag2 = Tag(name='第一章第二回') tag3 = Tag(name='第二章第一回') # 建立多对多关系 多表关系插入 表1.relationship.append(表2) article1.tag.append(tag1) article1.tag.append(tag2) article2.tag.append(tag1) article2.tag.append(tag3) article3.tag.append(tag2) article3.tag.append(tag3) # 插入数据库 db.session.add(article1) db.session.add(article2) db.session.add(article3) db.session.add(tag1) db.session.add(tag2) db.session.add(tag3) return '多对多插入成功'
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())
def createarticle(request): if request.method == "POST": try: user_id = request.session.get("user_id") title = request.POST.get("title") article = request.POST.get("article") #print(user_id) #print(title) #print(article) articles = Article() articles.a_user = User.objects.get(pk=user_id) articles.a_title = title articles.a_desc = article articles.save() data = {"msg": "ok", "status": 200} return JsonResponse(data=data) except: data = {"msg": "error"} #print("error") return render(request, "main/mine.html") #return JsonResponse(data=data) elif request.method == "GET": return render(request, "main/create_article.html")