Beispiel #1
0
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())
Beispiel #2
0
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())
Beispiel #3
0
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())