2017-01-23 41 views
0

我得到一個新的Django和LogicalDOC之間的休息Python的小問題。 我在LogicalDOC裏面創建一個文件夾,然後我想將這個pdf文件保存在folderId這個新文件夾中。插入數字作爲文件夾ID

但是,當它看起來工作,因爲從我的角度來看語法是好的:沒有PDF文件出現。

我創建一個文件夾,我拿起他的ID號碼:348930例如命令data["id"]和我插入str(data["id"])FolderId當我想保存我的pdf文件在新文件夾中。

新文件夾已創建並運行良好,但pdf文件不保存在裏面。有問題 ?

這是我的腳本:

@login_required 
def BirthCertificate_PDF(request, id) : 

    birthcertificate = get_object_or_404(BirthCertificate, pk=id) 

    data = {"birthcertificate" : birthcertificate} 

    template = get_template('BC_raw.html') 
    html = template.render(Context(data)) 


    filename_directory = str(BirthCertificate.objects.get(pk=id).lastname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).firstname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).birthday) 
    filename = 'Acte_Naissance_' + filename_directory + '.pdf' 
    path = '/Users/valentinjungbluth/Desktop/Django/Individus/' + filename 

    file = open(path, "w+b") 
    pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8') 

    file.seek(0) 
    pdf = file.read() 
    if pdf : 

     payload = '{{ "name":"{0}", "parentId":3309569 }}'.format(filename_directory) #Fix parent folder 

     url = 'http://localhost:8080/services/rest/folder/create' 
     headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} 
     resp = requests.post(url, data=payload, headers=headers, auth=('admin', 'admin')) 
     rbody = resp.content 
     data = json.loads(rbody) 

     print data["id"] #Get ID from the new folder 

     payload = '{{ "language":"fr","fileName":"{0}","FolderId":'+str(data["id"]) +'}}'.format(filename) #save pdf file inside the new folder thanks to his ID 
     upfile = path 
     files = { 
     'document': (None, payload, 'application/json'), 
     'content': (os.path.basename(upfile), open(upfile, 'rb'), 'application/octet-stream') 
     } 
     url = 'http://localhost:8080/services/rest/document/create' 
     headers = {'Content-Type': 'multipart/form-data'} 
     r = requests.post(url, files=files, headers=headers, auth=('admin', 'admin')) 

     context = {"birthcertificate":birthcertificate, 
        "path":path} 

     return render(request, 'BC_PDF.html', context) 
    file.close() 

    return HttpResponse(pdf, 'application/pdf') 

這是一個屏幕捕捉這表明folderID應該是:3538970 該號碼也由下式給出:data["id"]

enter image description here

+0

嘗試指定folderid作爲格式的第二個參數,例如:payload ='{{「language」:「fr」,「fileName」:「{0}」,「FolderId」:「{1} 「}}'。format(filename,str(data [」id「]))' – neverwalkaloner

+0

@neverwalkaloner仍然是同樣的錯誤,我的pdf文件沒有保存在剛剛創建的新文件夾中。我正在查看格式文檔。我在編寫它之前已經考慮了您的編輯;) – Deadpool

+0

確定它是一個語法錯誤:它是'folderId'而不是'FolderId' ...您的答案效果很好!正如之前:做出答案,我會驗證它? – Deadpool

回答

1

正如我所說的在我的評論中,您不需要使用字符串連接來將FolderId傳遞爲有效內容,只需使用第二個參數format方法:

d = '{{ "language":"fr","fileName":"{0}","FolderId":"{1}"}}'.format(‌​filename, str(data["id"])) 
+0

我真的不知道格式化方法的工作原理。所以謝謝你的幫助。 – Deadpool

相關問題